A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.
Input specifications
The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 ≤ A, B ≤ 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction.
The second line contains two integers, 1 ≤ N, M ≤ 100, denoting the numbers of robots and instructions respectively.
Then follow N lines with two integers, 1 ≤ Xi ≤ A, 1 ≤ Yi ≤ B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position.

Figure 1: The starting positions of the robots in the sample warehouse
Finally there are M lines, giving the instructions in sequential order. An instruction has the following format:
<robot #> <action> <repeat>
Where <action> is one of
- L: turn left 90 degrees,
- R: turn right 90 degrees, or
- F: move forward one meter,
Output specifications
Output one line for each test case:
- Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
- Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
- OK, if no crashing occurs.
Sample Input | Sample Output |
---|---|
4 5 4 2 2 1 1 E 5 4 W 1 F 7 2 F 7 5 4 2 4 1 1 E 5 4 W 1 F 3 2 F 1 1 L 1 1 F 3 5 4 2 2 1 1 E 5 4 W 1 L 96 1 F 2 5 4 2 3 1 1 E 5 4 W 1 F 4 1 L 1 1 F 20 | Robot 1 crashes into the wall Robot 1 crashes into robot 2 OK Robot 1 crashes into robot 2 |
Source: Nordic Collegiate Contest 2005
题解:一道比较鸡肋的模拟题,题目不难,根据题意一步一步模拟就好,注意细节把握。(这么水的题用了两个小时,调了半天才调对,coding能力还是不行呐)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
struct Robot{
int x,y;char p;
}robot[105];
struct Ins{
int r,num;char act;
}ins[105];
int main(){
bool flag;
int K;
cin >> K;
while (K--){
int A,B,N,M;
cin >> A >> B >> N >> M;
int map[105][105];
memset(map,0,sizeof(map));
for (int i = 1;i <= N;i++){
cin >> robot[i].x >> robot[i].y >> robot[i].p;
map[robot[i].x][robot[i].y] = i;
}
for (int j = 1;j <= M;j++) cin >> ins[j].r >> ins[j].act >> ins[j].num;
for (int j = 1;j <= M;j++){
flag = false;
//cin >> ins[j].r >> ins[j].act >> ins[j].num;
if (ins[j].act == 'L')
for (int i = 1;i <= ins[j].num;i++)
switch (robot[ins[j].r].p){
case 'N' : robot[ins[j].r].p = 'W'; break;
case 'W' : robot[ins[j].r].p = 'S'; break;
case 'S' : robot[ins[j].r].p = 'E'; break;
case 'E' : robot[ins[j].r].p = 'N'; break;
}
if (ins[j].act == 'R')
for (int i = 1;i <= ins[j].num;i++)
switch (robot[ins[j].r].p){
case 'N' : robot[ins[j].r].p = 'E'; break;
case 'W' : robot[ins[j].r].p = 'N'; break;
case 'S' : robot[ins[j].r].p = 'W'; break;
case 'E' : robot[ins[j].r].p = 'S'; break;
}
if (ins[j].act == 'F'){
for (int i = 1;i <= ins[j].num;i++){
if (robot[ins[j].r].p == 'E') {
if (robot[ins[j].r].x == A) {
printf("Robot %d crashes into the wall\n",ins[j].r);
flag = true;
break;
}
if (map[robot[ins[j].r].x+1][robot[ins[j].r].y]){
printf("Robot %d crashes into robot %d\n",
ins[j].r,map[robot[ins[j].r].x+1][robot[ins[j].r].y]);
flag = true;
break;
}else{
map[robot[ins[j].r].x+1][robot[ins[j].r].y] = ins[j].r;
map[robot[ins[j].r].x][robot[ins[j].r].y] = 0;
robot[ins[j].r].x++;
}
}
if (robot[ins[j].r].p == 'W') {
if (robot[ins[j].r].x == 1) {
printf("Robot %d crashes into the wall\n",ins[j].r);
flag = true;
break;
}
if (map[robot[ins[j].r].x-1][robot[ins[j].r].y]){
printf("Robot %d crashes into robot %d\n",
ins[j].r,map[robot[ins[j].r].x-1][robot[ins[j].r].y]);
flag = true;
break;
}
map[robot[ins[j].r].x-1][robot[ins[j].r].y] = ins[j].r;
map[robot[ins[j].r].x][robot[ins[j].r].y] = 0;
robot[ins[j].r].x--;
}
if (robot[ins[j].r].p == 'N') {
if (robot[ins[j].r].y == B) {
printf("Robot %d crashes into the wall\n",ins[j].r);
flag = true;
break;
}
if (map[robot[ins[j].r].x][robot[ins[j].r].y+1]){
printf("Robot %d crashes into robot %d\n",
ins[j].r,map[robot[ins[j].r].x][robot[ins[j].r].y+1]);
flag = true;
break;
}
map[robot[ins[j].r].x][robot[ins[j].r].y+1] = ins[j].r;
map[robot[ins[j].r].x][robot[ins[j].r].y] = 0;
robot[ins[j].r].y++;
}
if (robot[ins[j].r].p == 'S') {
if (robot[ins[j].r].y == 1) {
printf("Robot %d crashes into the wall\n",ins[j].r);
flag = true;
break;
}
if (map[robot[ins[j].r].x][robot[ins[j].r].y-1]){
printf("Robot %d crashes into robot %d\n",
ins[j].r,map[robot[ins[j].r].x][robot[ins[j].r].y-1]);
flag = true;
break;
}
map[robot[ins[j].r].x][robot[ins[j].r].y-1] = ins[j].r;
map[robot[ins[j].r].x][robot[ins[j].r].y] = 0;
robot[ins[j].r].y--;
}
}
}
if (flag) break;
}
if (!flag) cout <<"OK\n";
}
return 0;
}