题目链接。
分析:
虽说是简单的模拟,却调试了很长时间。
调试这么长时间总结来的经验:
1.坐标系要和题目建的一样,要不就会有各种麻烦。
2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a robot always completes its move before the next one starts moving。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 100 + 10; int G[maxn][maxn]; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; struct Robots{ int x, y, d; }r[100+10]; int main(){ int A, B, n, m, T, flag, rob, cra_rob; char s[5]; scanf("%d", &T); while(T--) { flag = 0; memset(G, 0, sizeof(G)); scanf("%d %d", &A, &B); scanf("%d %d", &n, &m); for(int i=1; i<=n; i++) { scanf("%d %d", &r[i].x, &r[i].y); scanf("%s", s); G[r[i].y][r[i].x] = i; switch(s[0]) { case 'N': r[i].d = 0; break; case 'E': r[i].d = 1; break; case 'S': r[i].d = 2; break; case 'W': r[i].d = 3; break; } } int num, rep; char act[3]; while(m--) { scanf("%d %s %d", &num, act, &rep); if(!flag) { if(act[0] == 'L') r[num].d = ((r[num].d - rep)%4+4)%4; else if(act[0] == 'R') r[num].d = (r[num].d + rep) % 4; else { G[r[num].y][r[num].x] = 0; for(int i=0; i<rep; i++) { r[num].x += dx[r[num].d]; r[num].y += dy[r[num].d]; if(r[num].x <= 0 || r[num].y <= 0 || r[num].x > A || r[num].y > B) { rob = num; flag = 1; break; } else if(G[r[num].y][r[num].x]) { rob = num; cra_rob = G[r[num].y][r[num].x]; flag = 2; break; } } if(!flag) G[r[num].y][r[num].x] = num; } } } if(flag == 1) printf("Robot %d crashes into the wall ", rob); else if(flag == 2) printf("Robot %d crashes into robot %d ", rob, cra_rob); else printf("OK "); } return 0; }