题目连接: POJ -- 2632ios
一道特别直观的模拟题,其实我感受模拟题仍是有用的,锻炼编程能力特别有帮助。编程
这道题目须要注意的地方在于转向的次数可能很是大,方向只有4种,对4取模再作处理就好。 还有就是这个题对坐标系懂了手脚,X轴方向不是南北而是东西。spa
po下代码:code
#include <cstdio> #include <iostream> #include <cstring> using namespace std; int robot[110][3]; int setdir(char c) { switch(c) { case 'N': return 0; case 'E': return 1; case 'S': return 2; case 'W': return 3; } } int g[110][110]; int main() { // freopen("D_in.txt", "r", stdin); int K; cin >> K; while(K--) { memset(g, 0, sizeof(g)); memset(robot, 0, sizeof(robot)); int N, M; scanf("%d%d", &N, &M); int A, B; scanf("%d%d", &A, &B); for(int i = 1; i <= A; i++) { int x, y; char dir; scanf("%d %d %c", &x, &y, &dir); robot[i][1] = x; robot[i][2] = y; robot[i][0] = setdir(dir); g[x][y] = i; } int ok = 1; for(int i = 1; i <= B; i++) { int id, t; char odr; scanf("%d %c %d", &id, &odr, &t); if(ok) switch(odr) { case 'L': t %= 4; robot[id][0] = (robot[id][0] + 4 - t) % 4; break; case 'R': robot[id][0] = (robot[id][0] + t) % 4; break; case 'F': int x = robot[id][1], y = robot[id][2]; int dir = robot[id][0], dx, dy; switch(dir) { case 0: dx = 0; dy = 1; break; case 1: dx = 1; dy = 0; break; case 2: dx = 0; dy = -1; break; case 3: dx = -1; dy = 0; break; } while(t--) { int nx = x + dx; int ny = y + dy; if(nx < 1 || nx > N || ny < 1 || ny > M) { printf("Robot %d crashes into the wall\n", id); ok = 0; break; } if(g[nx][ny]) { printf("Robot %d crashes into robot %d\n", id, g[nx][ny]); ok = 0; break; } g[nx][ny] = g[x][y]; g[x][y] = 0; x = nx; y = ny; } robot[id][1] = x; robot[id][2] = y; break; } } if(ok) printf("OK\n"); } return 0; }
一开始样例经过了,一直WAWAWA,后来请别人帮我看了一下,取模的时候我写成了t % 4(应该是t %= 4)。 给本身提个醒,多注意这种恶心的小错误。ci