Crashing Robots
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8576 | Accepted: 3703 |
Description
In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving.
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.
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
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 is one of
and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.
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 is one of
- L: turn left 90 degrees,
- R: turn right 90 degrees, or
- F: move forward one meter,
and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.
Output
Output one line for each test case:
Only the first crash is to be reported.
- 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.
Only the first crash is to be reported.
Sample Input
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
Sample Output
Robot 1 crashes into the wall Robot 1 crashes into robot 2 OK Robot 1 crashes into robot 2
题目大意:有A*B这么大的仓库,有N个机器人,每个机器人占一个格子,并且有一个初始方向,之后有M条指令,格式为
< robot #> < action> < repeat> 让#号机器人执行< action> 指令< repeat> 次,问是否会出现机器人相撞,或者撞墙。(不会出现两个机器人同时行动)
思路: 老老实实模拟,注意方向位置和大小写(Wa了7次),还有给的图并不是常见的X 0 to n, Y 0 to n注意存图的方式。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
#define inf 0xffffff
#define maxn 105
int dis[4][2] = {0,1,-1,0,0,-1,1,0};
struct Robot
{
int x,y,f;
}R[maxn];
struct Order
{
int a,b;
char c;
}O[maxn];
int Map[maxn][maxn],m,n,a,b,r1,r2;
bool flag;
void fun()
{
for(int i = 0; i < m; i++){
if(O[i].c == 'F'){
int xx = R[O[i].a].x;
int yy = R[O[i].a].y;
int ff = R[O[i].a].f;
Map[xx][yy] = 0;
for(int j = 0; j < O[i].b; j++){
xx += dis[ff][0];
yy += dis[ff][1];
if(Map[xx][yy] > 0) {flag = true; r1 = O[i].a,r2 = Map[xx][yy];break;}
else if( xx < 1 || xx > a || yy < 1 || yy > b) {flag = true; r1 = O[i].a,r2 = 0;break;}
}
if(flag) break;
R[O[i].a].x = xx;
R[O[i].a].y = yy;
Map[xx][yy] = O[i].a;
}
else if(O[i].c == 'R')
R[O[i].a].f = (R[O[i].a].f + 3 * O[i].b) % 4;
else if(O[i].c == 'L')
R[O[i].a].f = (R[O[i].a].f + O[i].b) % 4;
}
}
int main()
{
int k;
char dir;
scanf("%d",&k);
getchar();
while(k--){
flag = false;
memset(Map,0,sizeof(Map));
scanf("%d %d",&a, &b); getchar();
scanf("%d %d",&n, &m); getchar();
for(int i = 1; i <= n; i++){
scanf("%d %d %c",&R[i].x,&R[i].y,&dir);
getchar();
if(dir == 'N') R[i].f = 0;
else if(dir == 'W') R[i].f = 1;
else if(dir == 'S') R[i].f = 2;
else R[i].f = 3;
Map[R[i].x][R[i].y] = i;
}
for(int i = 0; i < m; i++) {scanf("%d %c %d",&O[i].a,&O[i].c,&O[i].b); getchar();}
fun();
if(flag){
if(r2 == 0) printf("Robot %d crashes into the wall\n",r1);
else printf("Robot %d crashes into robot %d\n",r1,r2);
}
else cout<<"OK"<<endl;
}
return 0;
}