coder_new

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

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


switch注意break,模拟机器人的走动,每移动一步判断一下
View Code
  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct Robot
  5 {
  6     int x,y;
  7     char dir;
  8 };
  9 Robot robot[100];
 10 
 11 struct Ins
 12 {
 13     int n;
 14     char ins;
 15     int re;
 16 };
 17 Ins ins[100];
 18 
 19 int hei,wid;
 20 int n_0,n_1,n_2;
 21 
 22 void check_f(Robot& robot)
 23 {
 24     switch(robot.dir)
 25     {
 26     case 'N': robot.y++;break;
 27     case 'S': robot.y--;break;
 28     case 'W': robot.x--;break;
 29     case 'E': robot.x++;break;
 30     }
 31 }
 32 
 33 void check_l(Robot& robot)
 34 {
 35     switch(robot.dir)
 36     {
 37     case 'N': robot.dir='W';break;
 38     case 'S': robot.dir='E';break;
 39     case 'E': robot.dir='N';break;
 40     case 'W': robot.dir='S';break;
 41     }
 42 }
 43 
 44 void check_r(Robot& robot)
 45 {
 46     switch(robot.dir)
 47     {
 48     case 'N': robot.dir='E';break;
 49     case 'S': robot.dir='W';break;
 50     case 'E': robot.dir='S';break;
 51     case 'W': robot.dir='N';break;
 52     }
 53 }
 54 
 55 int move(Robot* robot,int n_robot,char ins,int rep,int rob_n)
 56 {
 57     for(int i=0;i<rep;i++)
 58     {
 59         switch(ins)
 60         {
 61         case 'F':check_f(robot[n_robot]);break;
 62         case 'L':check_l(robot[n_robot]);break;
 63         case 'R':check_r(robot[n_robot]);break;
 64         }
 65         if(robot[n_robot].x<1||robot[n_robot].x>wid||robot[n_robot].y<1||robot[n_robot].y>hei) 
 66         {
 67             n_0=n_robot;
 68             return 1;
 69         }
 70 
 71         for(int i=0;i<rob_n;i++)
 72         {
 73             if((i!=n_robot)&&robot[i].x==robot[n_robot].x&&robot[i].y==robot[n_robot].y) 
 74             {
 75                 n_1=n_robot;
 76                 n_2=i;
 77                 return -1;
 78             }
 79         }
 80     }
 81     return 0;
 82 }
 83 
 84 int main()
 85 {
 86     int a;
 87     cin>>a;
 88     for(int i=0;i<a;i++)
 89     {
 90         cin>>wid;
 91         cin>>hei;
 92 
 93         int rob_n,ins_n;
 94         cin>>rob_n;
 95         cin>>ins_n;
 96 
 97         for(int i=0;i<rob_n;i++)
 98         {
 99             cin>>robot[i].x;
100             cin>>robot[i].y;
101             cin>>robot[i].dir;
102         }
103 
104         for(int i=0;i<ins_n;i++)
105         {
106             int t;
107             cin>>t;
108             ins[i].n=t-1;
109             cin>>ins[i].ins;
110             cin>>ins[i].re;
111         }
112 
113         bool flag=true;
114         for(int i=0;i<ins_n;i++)
115         {
116             switch(move(robot,ins[i].n,ins[i].ins,ins[i].re,rob_n))
117             {
118             case 1:
119                 {
120                     cout<<"Robot "<<n_0+1<<" crashes into the wall"<<endl;
121                     flag=false;
122                     break;
123                 }
124             case -1:
125                 {
126                     cout<<"Robot "<<n_1+1<<" crashes into robot "<<n_2+1<<endl;
127                     flag=false;
128                     break;
129                 }
130             case 0:;
131             }
132             if(flag==false) break;
133         }
134         if(flag==true) cout<<"OK"<<endl;
135 
136     }
137     return 0;
138 }

 


posted on 2012-05-10 17:33  coder_new  阅读(215)  评论(0编辑  收藏  举报