题目地址:http://poj.org/problem?id=2632
/*
题意:几个机器人按照指示,逐个朝某个(指定)方向的直走,如果走过的路上有机器人则输出谁撞到;如果走出界了,输出谁出界
如果以上都没发生,则输出OK
模拟题:无算法,按照条件编写几个函数和判断条件
注意:1. 关于绕转,只要模拟人的转圈方向,比如向右转就向右手边转(优化:当超过4次则对4取模)
2. 有先撞墙还是先撞机器人的问题(每一次'F'后都要及时判断是否OK)
3. 撞哪个机器人也有先来后到的顺序('F'后以这个机器人(除自己以外)遍历一边是否OK)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
struct NODE
{
int x, y;
int dir;
}node[MAXN];
struct OP
{
int name, t;
char move;
}op[MAXN];
bool flag; bool ok(int k, int N, int M, int n)
{
for (int i=; i<=n; ++i)
{
if (node[i].x < || node[i].x > N || node[i].y < || node[i].y > M)
{
printf ("Robot %d crashes into the wall\n", i);
flag = false;
return false;
}
} for (int j=; j<=n; ++j)
{
if (k != j && node[k].x == node[j].x && node[k].y == node[j].y)
{
printf ("Robot %d crashes into robot %d\n", k, j);
flag = false;
return false;
}
} flag = true;
return true;
} void forward(NODE *a, OP b, int N, int M, int n)
{
while (b.t--)
{
if (a->dir == 'N') a->x -= ;
else if (a->dir == 'S') a->x += ;
else if (a->dir == 'W') a->y -= ;
else a->y += ;
if (!ok (b.name, N, M, n)) break;
}
} NODE turn_l(NODE a, OP b)
{
b.t = (b.t >= ) ? (b.t % ) : b.t;
while (b.t--)
{
if (a.dir == 'N') a.dir = 'W';
else if (a.dir == 'S') a.dir = 'E';
else if (a.dir == 'E') a.dir = 'N';
else a.dir = 'S';
} return a;
} NODE turn_r(NODE a, OP b)
{
b.t = (b.t >= ) ? (b.t % ) : b.t;
while (b.t--)
{
if (a.dir == 'N') a.dir = 'E';
else if (a.dir == 'S') a.dir = 'W';
else if (a.dir == 'W') a.dir = 'N';
else a.dir = 'S';
} return a;
} void work(int N, int M, int n, int m)
{
int i;
for (i=; i<=m; ++i)
{
if (op[i].move == 'F')
forward (&node[op[i].name], op[i], N, M, n);
else if (op[i].move == 'L')
node[op[i].name] = turn_l (node[op[i].name], op[i]);
else if (op[i].move == 'R')
node[op[i].name] = turn_r (node[op[i].name], op[i]);
if (flag) continue;
else break;
}
if (i == m + ) puts ("OK");
} int main(void) //POJ 2632 Crashing Robots
{
//freopen ("G.in", "r", stdin); int t;
scanf ("%d", &t);
while (t--)
{
int N, M, n, m;
scanf ("%d%d", &M, &N);
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i)
{
scanf ("%d %d %c", &node[i].y, &node[i].x, &node[i].dir);
node[i].x = N + - node[i].x;
}
for (int i=; i<=m; ++i)
{
scanf ("%d %c %d", &op[i].name, &op[i].move, &op[i].t);
} flag = true;
work (N, M, n, m);
} return ;
} /*
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
*/