POJ 2632 Crashing Robots 模拟

来源:互联网 发布:m12标准螺距怎么编程 编辑:程序博客网 时间:2024/05/13 07:53
Crashing Robots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6698 Accepted: 2909

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.

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
  • 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:
  • 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

45 42 21 1 E5 4 W1 F 72 F 75 42 41 1 E5 4 W1 F 32 F 11 L 11 F 35 42 21 1 E5 4 W1 L 961 F 25 42 31 1 E5 4 W1 F 41 L 11 F 20

Sample Output

Robot 1 crashes into the wallRobot 1 crashes into robot 2OKRobot 1 crashes into robot 2
这道题是一道纯粹的模拟题,灰常简单,只不过有些题难,有些题绕,这道题需要很细心,对于机器人,处理好其位置,放心每次移动进行判断是否碰撞上其他机器人,每个命令执行完毕后判定该机器人是否碰上墙壁,这些方面注意以后这道题就会很容易的AC了,这么简单的题,竟然主页君还WA了一次,不应该啊,主页君第一次WA的主要原因是如果某一步机器人碰到墙壁了,没有阻止让机器人之间的碰撞的判定,造成如果判定之后机器人发生碰撞,记录了之后机器人的碰撞,而没有记录之前机器人撞墙,所以,这道题还是需要细心啊,把问题一修改,这道题立刻就AC了。。。很容易。
下面是AC代码:
#include<cstdio>#include<iostream>using namespace std;int a,b,n;struct robot{int x;int y;int fang;}ro[105];int judgero(int p){int i;for(i=1;i<=n;i++){if(i==p)continue;else{if(ro[p].x==ro[i].x&&ro[p].y==ro[i].y)return i;}}return 0;}int judge(int p){if(ro[p].x>0&&ro[p].y>0&&ro[p].x<=a&&ro[p].y<=b)return 0;return -1;}int main(){int t,i,j,m,p,q,flag,ma;char ch;cin>>t;while(t--){flag=0;cin>>a>>b;cin>>n>>m;for(i=1;i<=n;i++){scanf("%d%d %c",&ro[i].x,&ro[i].y,&ch);if(ch=='N')ro[i].fang=0;else if(ch=='E')ro[i].fang=1;else if(ch=='S')ro[i].fang=2;else if(ch=='W')ro[i].fang=3;}j=0;for(i=0;i<m;i++){scanf("%d %c %d",&p,&ch,&q);if(ch=='L'){while(q--){    ro[p].fang--;    if(ro[p].fang<0)    ro[p].fang=3;}}if(ch=='R'){while(q--){    ro[p].fang++;    if(ro[p].fang>3)    ro[p].fang=0;}}if(ch=='F'){while(q--){if(ro[p].fang==0)ro[p].y++;else if(ro[p].fang==1)ro[p].x++;else if(ro[p].fang==2)ro[p].y--;else if(ro[p].fang==3)ro[p].x--;if(j==0){   j=judgero(p);   if(j!=0)   {   flag=j;   ma=p;   }}}}if(judge(p)==-1&&flag==0){flag=-1;ma=p;j=1;}}if(flag==-1)printf("Robot %d crashes into the wall\n",ma);else if(flag==0)printf("OK\n");elseprintf("Robot %d crashes into robot %d\n",ma,flag);}return 0;}


原创粉丝点击