原题链接:
POJ-2632
大意:
多个机器人在网格内走,输入初始坐标,操作的机器人编号和移动方式。求机器人是否会撞墙或是相撞。
思路:
模拟题,唯一技巧就是都用结构体来写,把步骤分解成函数,一步步来写就不会出错。
代码实现:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
#define mem(s,t) memset(s,t,sizeof(s))
#define D(v) cout<<#v<<" "<<v<<endl
#define inf 0x3f3f3f3f
#define pb push_back
#define pii pair<int,int>
//#define LOCAL
const int N =110;
int next_directoin(int now_direction,char code){
if(code=='F') return now_direction;
else if(code=='L'){
return (now_direction+1)%4;
}else return (now_direction+3)%4;
}
int grid[N][N];
struct Robot{
int x,y,dir;
Robot(){}
Robot(int a,int b,int dir):x(a),y(b),dir(dir){}
}a[N];
char ch[10];
int toi[200];
int c,r,n,m,x,y;
int dy[]={1,0,-1,0};
int dx[]={0,-1,0,1};
int main() {
toi['N']=0,toi['W']=1,toi['S']=2,toi['E']=3;
int t;scanf("%d",&t);
while(t--){
mem(grid,0);
scanf("%d%d%d%d",&c,&r,&n,&m);
for(int i=1;i<=n;i++){
scanf("%d%d%s",&x,&y,ch);
a[i]=Robot(x,y,toi[ch[0]]);
grid[x][y]=i;
}
int bad=0;
for(int i=1;i<=m;i++){
int num,rep;
scanf("%d%s%d",&num,ch,&rep);
if(bad) continue;
while(rep--){
int now_direction=next_directoin(a[num].dir,ch[0]);
if(ch[0]=='F'){
int nx=dx[now_direction]+a[num].x;
int ny=dy[now_direction]+a[num].y;
if(nx<1||nx>c||ny<1||ny>r){
printf("Robot %d crashes into the wall\n",num);
bad=1;
break;
}else{
if(grid[nx][ny]){
printf("Robot %d crashes into robot %d\n",num,grid[nx][ny]);
bad=1;
break;
}else{
grid[a[num].x][a[num].y]=0;
grid[nx][ny]=num;
a[num]=Robot(nx,ny,now_direction);
}
}
}else{
a[num]=Robot(a[num].x,a[num].y,now_direction);
}
if(bad) break;
}
}
if(bad==0) puts("OK");
}
return 0;
}