题意:在一个 AXB 的平面有 N 个机器人,给出 N 个机器人的起始目标和移动方向,然后给出 M 条指令,指令格式为:< robot #> < action> < repeat>,依次为机器人编号、指令内容、执行次数。
指令内容:L 表示左转90度;R 表示右转90度;F 表示向前移动一步。
指令按顺序执行,执行完所有指令后,若过程中有机器人发生碰撞,输出第一次碰撞;若是没有发生碰撞,输出”OK“。
Output:
•Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
•Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
•OK, if no crashing occurs.分析:直接模拟…..
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 101;
const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
struct Robot {
int d, x, y;
}r[MAX];
int mp[MAX][MAX], A, B;
int main() {
char dir, op;
int t, n, m, id, cnt, crash;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d%d", &B, &A, &n, &m);
memset(mp, 0, sizeof(mp));
for(int i = 1; i <= n; i++) {
scanf("%d%d %c", &r[i].y, &r[i].x, &dir);
if(dir == 'W') { r[i].d = 0; }
else if(dir == 'N') { r[i].d = 1; }
else if(dir == 'E') { r[i].d = 2; }
else { r[i].d = 3; }
mp[r[i].x][r[i].y] = i;
}
crash = 0;
for(int i = 0; i < m; i++) {
scanf("%d %c%d", &id, &op, &cnt);
if(crash) { continue; }
Robot &rb = r[id];
if(op != 'F') { cnt %= 4; }
for(int j = 0; j < cnt; j++) {
if(op == 'L') {
rb.d--;
if(rb.d < 0) {
rb.d += 4;
}
}
else if(op == 'R') {
rb.d++;
if(rb.d > 3) {
rb.d -= 4;
}
}
else {
int nx = rb.x + dx[rb.d], ny = rb.y + dy[rb.d];
if(mp[nx][ny]) {
printf("Robot %d crashes into robot %d\n", id, mp[nx][ny]);
crash = 1;
break;
}
else if(nx > A || ny > B || !nx || !ny) {
printf("Robot %d crashes into the wall\n", id);
crash = 1;
break;
}
else {
mp[rb.x][rb.y] = 0;
mp[rb.x = nx][rb.y = ny] = id;
}
}
}
}
if(!crash) { puts("OK"); }
}
return 0;
}