http://poj.org/problem?id=2632
坐标的n,m与编程的相反....
方向的N,S也要注意是相反的
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <iostream> using namespace std; __int64 inf=15; double eps=0.000001; int mp[105][105]; struct node { int y,x; char dir; }; node robot[105]; int done; int xx,yy; void turnR(int who) { if (robot[who].dir=='N') robot[who].dir='E'; else if (robot[who].dir=='W') robot[who].dir='N'; else if (robot[who].dir=='S') robot[who].dir='W'; else if (robot[who].dir=='E') robot[who].dir='S'; } void turnl(int who) { if (robot[who].dir=='N') robot[who].dir='W'; else if (robot[who].dir=='W') robot[who].dir='S'; else if (robot[who].dir=='S') robot[who].dir='E'; else if (robot[who].dir=='E') robot[who].dir='N'; } void goford(int who) { int x=robot[who].x; int y=robot[who].y; if (robot[who].dir=='S') { if (x-1<=0) {printf("Robot %d crashes into the wall\n",who); done=1;return ;} else if (mp[x-1][y]) {printf("Robot %d crashes into robot %d\n",who,mp[x-1][y]); done=1;return ;} else {mp[x][y]=0;robot[who].x--;mp[x-1][y]=who;} } if (robot[who].dir=='W') { if (y-1<=0) {printf("Robot %d crashes into the wall\n",who); done=1;return ;} else if (mp[x][y-1]) {printf("Robot %d crashes into robot %d\n",who,mp[x][y-1]); done=1;return ;} else {mp[x][y]=0;robot[who].y--;mp[x][y-1]=who;} } if (robot[who].dir=='N') { if (x+1>yy) {printf("Robot %d crashes into the wall\n",who); done=1;return ;} else if (mp[x+1][y]) {printf("Robot %d crashes into robot %d\n",who,mp[x+1][y]); done=1;return ;} else {mp[x][y]=0;robot[who].x++;mp[x+1][y]=who;} } if (robot[who].dir=='E') { if (y+1>xx) {printf("Robot %d crashes into the wall\n",who); done=1;return ;} else if (mp[x][y+1]) {printf("Robot %d crashes into robot %d\n",who,mp[x][1+y]); done=1;return ;} else {mp[x][y]=0;robot[who].y++;mp[x][y+1]=who;} } } int main() { int t; // freopen ("F:\\duipai\\1.out","w",stdout); cin>>t; while(t--) { memset(mp,0,sizeof(mp)); int n,m; int i; cin>>xx>>yy; cin>>n>>m; for (i=1;i<=n;i++) { scanf("%d %d %c\n",&robot[i].y,&robot[i].x,&robot[i].dir); mp[robot[i].x][robot[i].y]=i; } char ml; int who,times; done=0; for (i=1;i<=m;i++) { scanf("%d %c %d",&who,&ml,×); if (done) continue; while(times--) { if (ml=='L') turnl(who); if (ml=='R') turnR(who); if (ml=='F') goford(who); if (done) break; } // printf("----%d--%d--%c\n",robot[1].x,robot[1].y,robot[1].dir); } if (!done) printf("OK\n"); } return 0; }