題目連接: 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