poj_2632 构造

思路:构造模型

WA了好长时间,这题没有算法,需要的是仔细考虑+耐心啊。


<span style="font-size:18px;">#include<iostream>
#include<fstream> 
using namespace std;

struct Robots{
	int x;
	int y;
	int z;
};

int main()
{
	//ifstream infile("test.txt"); 
	int t,num,i,j;
	cin>>num;
	int map_length,map_width;
	for(t=0;t<num;t++)
	{
		int map[105][105]={0};
		Robots robots[101];
		cin>>map_width>>map_length;//5,4

		int rob_num,instr_num;
		cin>>rob_num>>instr_num;

		int x,y;
		char z;
		//set boarder
		for(i=0;i<map_width+1;i++)
		{
			map[0][i]= -1;
			map[map_length+1][i]= -1;
		}
		for(i=0;i<map_length+1;i++)
		{
			map[i][0]= -1;
			map[i][map_width+1]= -1;
		}
		//input robots and start_direction: N=1,E=2,S=3,W=4
		for(i=0;i<rob_num;i++)
		{
			cin>>x>>y>>z;
			switch(z)
			{
				case 'N':
					robots[i].x=map_length-y+1;
					robots[i].y=x;
					robots[i].z=1;
					map[map_length-y+1][x]=1;
					break;
				case 'E':
					robots[i].x=map_length-y+1;
					robots[i].y=x;
					robots[i].z=2;
					map[map_length-y+1][x]=2;
					break;
				case 'S':
					robots[i].x=map_length-y+1;
					robots[i].y=x;
					robots[i].z=3;
					map[map_length-y+1][x]=3;
					break;
				case 'W':
					robots[i].x=map_length-y+1;
					robots[i].y=x;
					robots[i].z=4;
					map[map_length-y+1][x]=4;
					break;
			}
		}
		//input instructions
		int rob,rep;
		char act;
		int res=0;
		int object1=0,object2=0;
		for(i=0;i<instr_num;i++)
		{
			cin>>rob>>act>>rep;
			rob=rob-1;
			if(res!=0)
			{
				continue;
			}
			//execute this instruction
			switch(act)
			{
				case 'L':
					//turn left
					for(j=0;j<rep;j++)
					{
						robots[rob].z-=1;
						if(robots[rob].z==0)
						{
							robots[rob].z=4;
						}						
					}
					break;
				case 'R':
					//turn right
					for(j=0;j<rep;j++)
					{
						robots[rob].z+=1;
						if(robots[rob].z==5)
						{
							robots[rob].z=1;
						}
					}
					break;
				case 'F':
					//execute this instruction rep times
					for(j=0;j<rep && res==0;j++)
					{
						switch(robots[rob].z)
						{
							case 1:
								map[robots[rob].x][robots[rob].y]=0;
								robots[rob].x-=1;
								//judge whether crash other robots
								for(int m=0;m<rob_num;m++)
								{
									if(m==rob)
									{
										continue;
									}
									if((robots[rob].x==robots[m].x)&&(robots[rob].y==robots[m].y))
									{
										object1=rob;
										object2=m;
										res=2;
										break;
									}
								}
								//judge whether crash the wall
								if(res==0&&map[robots[rob].x][robots[rob].y]==-1)
								{
									object1=rob;
									res=1;
									break;
								}
								map[robots[rob].x][robots[rob].y]=1;
								break;
							case 2:
								map[robots[rob].x][robots[rob].y]=0;
								robots[rob].y+=1;
								//judge whether crash other robots
								for(int m=0;m<rob_num;m++)
								{
									if(m==rob)
									{
										continue;
									}
									if((robots[rob].x==robots[m].x)&&(robots[rob].y==robots[m].y))
									{
										object1=rob;
										object2=m;
										res=2;
										break;
									}
								}
								//judge whether crash the wall
								if(res!=2&&map[robots[rob].x][robots[rob].y]==-1)
								{
									object1=rob;
									res=1;
									break;
								}
								map[robots[rob].x][robots[rob].y]=2;
								break;
							case 3:
								map[robots[rob].x][robots[rob].y]=0;
								robots[rob].x+=1;
								//judge whether crash other robots
								for(int m=0;m<rob_num;m++)
								{
									if(m==rob)
									{
										continue;
									}
									if((robots[rob].x==robots[m].x)&&(robots[rob].y==robots[m].y))
									{
										object1=rob;
										object2=m;
										res=2;
										break;
									}
								}
								//judge whether crash the wall
								if(res!=2&&map[robots[rob].x][robots[rob].y]==-1)
								{
									object1=rob;
									res=1;
									break;
								}
								map[robots[rob].x][robots[rob].y]=3;
								break;
							case 4:
								map[robots[rob].x][robots[rob].y]=0;
								robots[rob].y-=1;
								//judge whether crash other robots
								for(int m=0;m<rob_num;m++)
								{
									if(m==rob)
									{
										continue;
									}
									if((robots[rob].x==robots[m].x)&&(robots[rob].y==robots[m].y))
									{
										object1=rob;
										object2=m;
										res=2;
										break;
									}
								}
								//judge whether crash the wall
								if(res!=2&&map[robots[rob].x][robots[rob].y]==-1)
								{
									object1=rob;
									res=1;
									break;
								}
								map[robots[rob].x][robots[rob].y]=4;
								break;
						}//end switch
					}//end one instruction
					break;
			}//end one instruction
		}//end for:instruction
		if(res==0)
		{
			cout<<"OK"<<endl;
		}
		else if(res==1)
		{
			cout<<"Robot "<<object1+1<<" crashes into the wall"<<endl;
		}
		else if(res==2)
		{
			cout<<"Robot "<<object1+1<<" crashes into robot "<<object2+1<<endl;
		}
	}
	//system("pause");
	return 0;
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值