题目大意:就是将坐标轴拟成二维矩阵的形式,E,N,W,S用0,1,2,3表示方便计算,左转右转后的方向用mod4来算。还有就是用编号作下标表示坐标,和用坐标作下标表示编号。更新一个点时,要将前一个清空。 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { //freopen("temp.txt", "r", stdin); int t; int a, b; //网格大小 int m, n; //robot数, 步数 int xx[101], yy[101]; //编号作下标, 记录坐标 int num[101][101]; //坐标作下标, 记录编号 int dir[101][101]; //记录坐标上robot的方向 int rob[101], rep[101]; //robot编号, 重复数 char fwd[101]; //方向 bool flag; cin >> t; while (t--) { flag = false; memset(xx, 0, sizeof(xx)); memset(yy, 0, sizeof(yy)); memset(num, 0, sizeof(num)); memset(dir, 0, sizeof(dir)); cin >> a >> b; cin >> m >> n; int x, y; char direct; for (int i = 1; i <= m; ++i) { cin >> x >> y >> direct; xx[i] = x; yy[i] = y; num[x][y] = i; if (direct == 'E') // 0 dir[x][y] = 0; else if (direct == 'N') // 90 dir[x][y] = 1; else if (direct == 'W') // 180 dir[x][y] = 2; else if (direct == 'S') // 360 dir[x][y] = 3; } for (int i = 1; i <= n; ++i) { cin >> rob[i] >> fwd[i] >> rep[i]; } for (int i = 1; i <= n; ++i) { x = xx[rob[i]]; y = yy[rob[i]]; if (fwd[i] == 'L') { dir[x][y] = (dir[x][y]+rep[i]) % 4; } else if (fwd[i] == 'R') { dir[x][y] = (dir[x][y]-rep[i]) % 4; if (dir[x][y] < 0) dir[x][y] += 4; } else if (fwd[i] == 'F') { if (dir[x][y] == 0) {//E for (int j = 1; j <= rep[i]; ++j) { if (num[x+j][y]) { //有robot cout << "Robot " << num[x][y] << " crashes into robot " << num[x+j][y] << endl; flag = true; break; } if (x+j > a) { cout << "Robot " << num[x][y] << " crashes into the wall" << endl; flag = true; break; } if (j == rep[i]) { num[x][y] = 0; xx[rob[i]] += j; dir[x+j][y] = 0; num[x+j][y] = rob[i]; } } } else if (dir[x][y] == 1) { for (int j = 1; j <= rep[i]; ++j) { if (num[x][y+j]) { //有robot cout << "Robot " << num[x][y] << " crashes into robot " << num[x][y+j] << endl; flag = true; break; } if (y+j > b) { cout << "Robot " << num[x][y] << " crashes into the wall" << endl; flag = true; break; } if (j == rep[i]) { num[x][y] = 0; yy[rob[i]] += j; dir[x][y+j] = 1; num[x][y+j] = rob[i]; } } } else if (dir[x][y] == 2) { for (int j = 1; j <= rep[i]; ++j) { if (num[x-j][y]) { //有robot cout << "Robot " << num[x][y] << " crashes into robot " << num[x-j][y] << endl; flag = true; break; } if (x-j < 1) { cout << "Robot " << num[x][y] << " crashes into the wall" << endl; flag = true; break; } if (j == rep[i]) { num[x][y] = 0; xx[rob[i]] -= j; dir[x-j][y] = 2; num[x-j][y] = rob[i]; } } } else if (dir[x][y] == 3) { for (int j = 1; j <= rep[i]; ++j) { if (num[x][y-j]) { //有robot cout << "Robot " << num[x][y] << " crashes into robot " << num[x][y-j] << endl; flag = true; break; } if (y-j < 1) { cout << "Robot " << num[x][y] << " crashes into the wall" << endl; flag = true; break; } if (j == rep[i]) { num[x][y] = 0; yy[rob[i]] -= j; dir[x][y-j] = 3; num[x][y-j] = rob[i]; } } } } if (flag) break; } if (!flag) cout << "OK" << endl; } return 0; }