poj 2632 Crashing Robots【模拟】【略坑~】【刷题计划】
Crashing Robots
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11233 | Accepted: 4753 |
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
题意:读入t组数据,每组读入地图的列数c和行数r,再读入n和m,接下来输入n行(机器人编号为1——n),每行输入机器人的起始位置x,y,和起始方向,
最后输入m行,每行输入一条命令,命令内容包括机器人的号数numebr和机器人的方向direction以及该条命令的执行次数step。
如果有两个机器人相撞,按题目输出,如果有机器人越界,按题目输出,如果没有撞击产生,输出OK。
题目需要注意的地方是:
1.只需要输出第一次相撞的情况.
2.机器人执行向左或向右的step次命令后,还是在原地,只是方向不停改变(这点好坑啊,自己理解错了,以为是向左转走step步)
3.由于二维数组建图后坐标原点也会不同,所以我们的方向也需要改变,具体依照自己的习惯建图就好(这个容易晕,最好先在草稿上写好)
思路:
从东方E开始把方向逆时针标记为0~3,向左时,方向+1,向右时,方向-1,判断存储地图的二维数组在相应坐标下是否有机器人即可判断两机器人是否相撞。。。。嗯嗯,感觉琐碎啊,还是看注释好了
-------------------
老师:每次写题前想想出题人的意图。
我:大概出模拟题的出题人都是想让我死。
还是保命要紧。
#include<stdio.h> #include<string.h> #define N 110 struct node{ int x,y,dir; }num[N]; struct dir{ int number,step; char direction; }str[N]; int main() { int t,i,j; int map[N][N]; int k[4][2] = {0,1,1,0,0,-1,-1,0}; int n,m,c,r,step,number,flag; char direction; scanf("%d",&t); while(t--) { scanf("%d%d",&c,&r); scanf("%d%d",&n,&m); memset(map,0,sizeof(map)); for(i = 1; i <= n; i ++) { scanf("%d %d %c",&num[i].y,&num[i].x ,&direction); map[num[i].x][num[i].y] = i;//机器人未移动时,地图上标记为机器人的号数 if(direction == 'E') num[i].dir = 0; else if(direction == 'N') num[i].dir = 1; else if(direction == 'W') num[i].dir = 2; else if(direction == 'S') num[i].dir = 3; } for(i = 1; i <= m; i ++) scanf("%d %c %d",&str[i].number,&str[i].direction ,&str[i].step ); flag = 0;//标记机器人是否相撞 for(i = 1; i <= m; i ++) { direction = str[i].direction ; number = str[i].number ; step = str[i].step ; if(direction == 'L')//执行左转命令 { for(j = 1; j <= step; j ++) num[number].dir = (num[number].dir+1)%4; } else if(direction == 'R')//执行右转命令 for(j = 1; j <= step; j ++) num[number].dir = (num[number].dir-1+4)%4; if(step > 0&&direction == 'F')//当移动步数大于1时 { map[num[number].x][num[number].y] = 0;//地图上标记的机器人位置清空 for(j = 1; j <= step; j ++) { num[number].x += k[num[number].dir][0]; num[number].y += k[num[number].dir][1]; if(map[num[number].x][num[number].y] != 0)//地图上该处不为空,说明有机器人 { flag = 1; break; } else if(num[number].x < 1||num[number].x > r||num[number].y < 1||num[number].y > c) {//越界 flag = 2; break; } } if(flag == 1)//如果机器人相撞 { printf("Robot %d crashes into robot %d\n",number,map[num[number].x][num[number].y]); break; } else if(flag == 2)//如果机器人撞到墙 { printf("Robot %d crashes into the wall\n",number); break; } } map[num[number].x][num[number].y ] = number;//地图上机器人的停止点标记为机器人号数 } if(!flag) printf("OK\n"); } return 0; }