思路:构造模型
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>