您的位置:首页 > 其它

pku2632 Crashing Robots

2010-03-03 17:22 169 查看
题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=2632

题意简述:一个若干个机器人,在一个n*m的矩形框内,现给定若干种操作,求能否完成这些操作(不能则输出中途出现了的状况)。

解题思路:模拟,应该没什么trick吧,样例已经很具有代表性了。

代码:

#include<stdio.h>
#include<string.h>
using namespace std;
const int Max=105;
int map[Max][Max];
struct Robots{
int x,y;
int ii;
}robot[Max];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(map,0,sizeof(map));
int a,b;
scanf("%d%d",&a,&b);
int n,m;
scanf("%d%d",&n,&m);
int x,y;
char ff;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
cin>>ff;
robot[i].x=x;
robot[i].y=y;
map[x][y]=i;
switch(ff)
{
case 'E':robot[i].ii=1;break;
case 'N':robot[i].ii=2;break;
case 'W':robot[i].ii=3;break;
case 'S':robot[i].ii=4;break;
}
}
int cc=0;
int s=0,e=0;
bool yes=false;
for(int i=1;i<=m;i++)
{
int r,num;
char c;
cin>>r>>c>>num;
if(yes) continue;
int xx=robot[r].x;
int yy=robot[r].y;
bool flag;
if(c=='F')
{
if(robot[r].ii==1)
{
if(robot[r].x+num>a)
{
flag=false;
for(int j=xx+1;j<=a;j++)
if(map[j][yy]!=0)
{
s=r;
e=map[j][yy];
yes=true;
flag=true;
break;
}
if(flag) continue;
cc=r;
yes=true;
continue;
}
flag=false;
for(int j=xx+1;j<=xx+num;j++)
if(map[j][yy]!=0)
{
s=r;
e=map[j][yy];
yes=true;
flag=true;
continue;
}
if(!flag)
{
map[xx][yy]=0;
map[xx+num][yy]=r;
robot[r].x=xx+num;
robot[r].y=yy;
}
continue;
}
if(robot[r].ii==3)
{
if(robot[r].x-num<1)
{
flag=false;
for(int j=xx-1;j>=1;j--)
if(map[j][yy]!=0)
{
s=r;
e=map[j][yy];
yes=true;
flag=true;
break;
}
if(flag) continue;
cc=r;
yes=true;
continue;
}
flag=false;
for(int j=xx-1;j>=xx-num;j--)
if(map[j][yy]!=0)
{
s=r;
e=map[j][yy];
flag=true;
yes=true;
continue;
}
if(!flag)
{
map[xx][yy]=0;
map[xx-num][yy]=r;
robot[r].x=xx-num;
robot[r].y=yy;
}
continue;
}
if(robot[r].ii==2)
{
if(robot[r].y+num>b)
{
flag=false;
for(int j=yy+1;j<=b;j++)
if(map[xx][j]!=0)
{
s=r;
e=map[xx][j];
yes=true;
flag=true;
break;
}
if(flag) continue;
cc=r;
yes=true;
continue;
}
flag=false;
for(int j=yy+1;j<=yy+num;j++)
if(map[xx][j]!=0)
{
s=r;
e=map[xx][j];
flag=true;
yes=true;
continue;
}
if(!flag)
{
map[xx][yy]=0;
map[xx][yy+num]=r;
robot[r].x=xx;
robot[r].y=yy+num;
}
continue;
}
if(robot[r].ii==4)
{
if(robot[r].y-num<1)
{
flag=false;
for(int j=yy-1;j>=1;j--)
if(map[xx][j]!=0)
{
s=r;
e=map[xx][j];
yes=true;
flag=true;
break;
}
if(flag) continue;
cc=r;
yes=true;
continue;
}
flag=false;
for(int j=yy-1;j>=yy-num;j--)
if(map[xx][j]!=0)
{
s=r;
e=map[xx][j];
flag=true;
yes=true;
continue;
}
if(!flag)
{
map[xx][yy]=0;
map[xx][yy-num]=r;
robot[r].x=xx;
robot[r].y=yy-num;
}
continue;
}
}
if(c=='L')
{
num%=4;
robot[r].ii+=num;
robot[r].ii=(robot[r].ii-1)%4+1;
continue;
}
if(c=='R')
{
num%=4;
robot[r].ii+=4;
robot[r].ii-=num;
robot[r].ii=(robot[r].ii-1)%4+1;
continue;
}

}
if(cc!=0)
printf("Robot %d crashes into the wall/n",cc);
else if(s&&e) printf("Robot %d crashes into robot %d/n",s,e);
else printf("OK/n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: