http://poj.org/problem?id=2632
模拟起来还是蛮麻烦的。
不过总比一堆函数好。
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int dx[] = {-1, 0, +1, 0, -1};
int dy[] = {-1, +1, 0, -1, 0};
struct _1Nd {
int id;
int dir;
_1Nd() {
id = 0;
dir = 0;
}
} _1nd[105][105];
int a, b;
bool out;
int rx[105], ry[105];
void show() {
for(int i = 1; i <= a; ++i) {
for(int j = 1; j <= b; ++j) {
printf("%d", _1nd[i][j].dir);
}
printf("\n");
}
printf("---\n");
}
void Move(int id, int w) {
if(out == 1)
return;
while(w--) {
//printf("w=%d\n",w);
int i = rx[id], j = ry[id];
int di = dx[_1nd[i][j].dir];
int dj = dy[_1nd[i][j].dir];
if(i + di < 1 || j + dj < 1 || i + di > a || j + dj > b) {
out = 1;
printf("Robot %d crashes into the wall\n", _1nd[i][j].id);
return;
} else {
if(_1nd[i + di][j + dj].id) {
out = 1;
printf("Robot %d crashes into robot %d\n", _1nd[i][j].id, _1nd[i + di][j + dj].id);
return;
} else {
_1nd[i + di][j + dj] = _1nd[i][j];
_1nd[i][j] = _1nd[0][0];
}
}
rx[id] += di;
ry[id] += dj;
//show();
}
}
void Rotate(int id, int w, int d) {
if(out == 1)
return;
int i = rx[id], j = ry[id];
w %= 4;
while(w--) {
_1nd[i][j].dir += d;
if(_1nd[i][j].dir == 5)
_1nd[i][j].dir = 1;
if(_1nd[i][j].dir == 0)
_1nd[i][j].dir = 4;
//show();
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int t;
scanf("%d", &t);
while(t--) {
out = 0;
scanf("%d%d", &b, &a);
for(int i = 1; i <= a; ++i) {
for(int j = 1; j <= b; ++j)
_1nd[i][j] = _1nd[0][0];
}
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &ry[i], &rx[i]);
rx[i] = a - rx[i] + 1;
_1nd[rx[i]][ry[i]].id = i;
char dir[2];
scanf("%s", dir);
if(dir[0] == 'E')
_1nd[rx[i]][ry[i]].dir = 1;
else if(dir[0] == 'S')
_1nd[rx[i]][ry[i]].dir = 2;
else if(dir[0] == 'W')
_1nd[rx[i]][ry[i]].dir = 3;
else
_1nd[rx[i]][ry[i]].dir = 4;
}
//show();
while(m--) {
int id, w;
char op[2];
scanf("%d%s%d", &id, op, &w);
if(op[0] == 'F') {
Move(id, w);
} else if(op[0] == 'L') {
Rotate(id, w, -1);
} else {
Rotate(id, w, 1);
}
}
if(out == 0)
puts("OK");
}
}