标签:des c style class blog code
Crashing Robots
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
题目大意:给定一个A*B的棋盘,N个机器人,每个机器人都有起始位置,M个指令(x,C,r)代表第x个机器人执行指令C重复r次。
F->向前走一步
L->向左转
R->向右转
若i号机器人撞墙,输出:Robot i crashes into the wall
若i号机器人撞到j号机器人,输出:Robot i
crashes into robot j
若M个指令执行完仍无事故发生 输出:OK
结题思路:模拟,写的比较长。。。
Code:
1 #include<string> 2 #include<iostream> 3 #include<stdio.h> 4 #define INTO_WALL 0 5 #define INTO_ROBIT -1 6 #define SAFE 1 7 using namespace std; 8 struct Robit 9 { 10 int x,y; 11 int dir; 12 } R[10000]; 13 int date1,date2,N,T,M,A,B;; 14 void step(int i,int dir) 15 { 16 if (dir==1) R[i].y++; 17 if (dir==2) R[i].x++; 18 if (dir==3) R[i].y--; 19 if (dir==4) R[i].x--; 20 } 21 int move(int i,char dir,int dis) 22 { 23 if (dir==‘L‘) 24 for (int j=1; j<=dis; j++) 25 R[i].dir=(R[i].dir-1)?(R[i].dir-1):4; 26 else if (dir==‘R‘) 27 for (int j=1; j<=dis; j++) 28 R[i].dir=(R[i].dir-4)?(R[i].dir+1):1; 29 else 30 { 31 for (int j=1; j<=dis; j++) 32 { 33 step(i,R[i].dir); 34 if ((R[i].x>=A+1||R[i].x<=0)||(R[i].y>=B+1||R[i].y<=0)) 35 { 36 date1=i; 37 return INTO_WALL; 38 } 39 for (int k=1; k<=N; k++) 40 { 41 if (k==i) continue; 42 if (R[k].x==R[i].x&&R[k].y==R[i].y) 43 { 44 date1=i,date2=k; 45 return INTO_ROBIT; 46 } 47 } 48 } 49 } 50 return SAFE; 51 } 52 int main() 53 { 54 int flag=0,tmp,meter,ok,i; 55 char tdir,ctmp; 56 cin>>T; 57 while (T--) 58 { 59 flag=0; 60 cin>>A>>B; 61 cin>>N>>M; 62 for (i=1; i<=N; i++) 63 { 64 cin>>R[i].x>>R[i].y>>ctmp; 65 if (ctmp==‘N‘) R[i].dir=1; 66 if (ctmp==‘E‘) R[i].dir=2; 67 if (ctmp==‘S‘) R[i].dir=3; 68 if (ctmp==‘W‘) R[i].dir=4; 69 } 70 for (i=1; i<=M; i++) 71 { 72 cin>>tmp>>tdir>>meter; 73 if (flag) continue; 74 ok=move(tmp,tdir,meter); 75 if (ok==SAFE) continue; 76 else if (ok==INTO_ROBIT) 77 { 78 printf("Robot %d crashes into robot %d\n",date1,date2); 79 flag=1; 80 } 81 else if (ok==INTO_WALL) 82 { 83 printf("Robot %d crashes into the wall\n",date1); 84 flag=1; 85 } 86 } 87 if (!flag) printf("OK\n"); 88 } 89 return 0; 90 }
POJ2632——Crashing Robots,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/Enumz/p/3764111.html