hdu 三部曲 Crashing Robots

女爷i 2022-01-05 15:34 403阅读 0赞

Problem 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.
2632_1.jpg
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

***************************************************************************************************************************

大模拟,没技巧,细节很重要。

*******************************************************************************************************************************

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<iostream>
  2. 2 #include<string>
  3. 3 #include<cstring>
  4. 4 #include<cstdio>
  5. 5 #include<cctype>
  6. 6 #include<queue>
  7. 7 #include<stack>
  8. 8 using namespace std;
  9. 9 struct node
  10. 10 {
  11. 11 int x,y;
  12. 12 char id;
  13. 13 }robot[1001];
  14. 14 int a,b,n,m,case1,i,j,k;
  15. 15 int mop[1001][1001];
  16. 16 void init()
  17. 17 {
  18. 18 cin>>a>>b>>n>>m;
  19. 19 for(i=1;i<=b;i++)
  20. 20 for(j=1;j<=a;j++)
  21. 21 mop[i][j]=0;
  22. 22 int xx,yy;
  23. 23 char de;
  24. 24 for(i=1;i<=n;i++)//此处反一下
  25. 25 {
  26. 26 cin>>xx>>yy>>de;
  27. 27 //getchar();
  28. 28 robot[i].x=xx;
  29. 29 robot[i].y=b-yy+1;
  30. 30 robot[i].id=de;
  31. 31 //getchar();
  32. 32 mop[b-yy+1][xx]=i;
  33. 33 }
  34. 34
  35. 35 }
  36. 36 void sovle()
  37. 37 {
  38. 38 init();
  39. 39 int num,times;
  40. 40 char dest;
  41. 41 int cur,k=0;
  42. 42 int flag=1;
  43. 43 while(m--)
  44. 44 {
  45. 45 cin>>num>>dest>>times;
  46. 46 /*getchar();
  47. 47 cin>>dest;
  48. 48 getchar();
  49. 49 cin>>times;
  50. 50 */
  51. 51 if(flag==0)continue;
  52. 52 else
  53. 53 {
  54. 54 cur=num;
  55. 55 for(i=1;i<=times;i++)
  56. 56 {
  57. 57 if(flag==0)break;
  58. 58 if(dest=='F')//直走
  59. 59 {
  60. 60 if(robot[num].id=='N')
  61. 61 {
  62. 62 mop[robot[num].y][robot[num].x]=0;
  63. 63 robot[num].y--;
  64. 64 if(robot[num].y<=0)
  65. 65 {
  66. 66 flag=0;
  67. 67
  68. 68 }
  69. 69
  70. 70 else
  71. 71 {
  72. 72 if(mop[robot[num].y][robot[num].x]==0)
  73. 73 mop[robot[num].y][robot[num].x]=num;
  74. 74 else
  75. 75 {
  76. 76 k=mop[robot[num].y][robot[num].x];
  77. 77 flag=0;
  78. 78 }
  79. 79 }
  80. 80 }
  81. 81 else
  82. 82 if(robot[num].id=='S')
  83. 83 {
  84. 84 mop[robot[num].y][robot[num].x]=0;
  85. 85 robot[num].y++;
  86. 86 if(robot[num].y>b)
  87. 87 {
  88. 88 flag=0;
  89. 89 //break;
  90. 90 }
  91. 91 else
  92. 92 {
  93. 93 if(mop[robot[num].y][robot[num].x]!=0)
  94. 94 {
  95. 95 k=mop[robot[num].y][robot[num].x];
  96. 96 flag=0;
  97. 97 }
  98. 98 else
  99. 99 mop[robot[num].y][robot[num].x]=num;
  100. 100 }
  101. 101 }
  102. 102 else
  103. 103 if(robot[num].id=='E')
  104. 104 {
  105. 105 mop[robot[num].y][robot[num].x]=0;
  106. 106 robot[num].x++;
  107. 107 if(robot[num].x>a)
  108. 108 {
  109. 109 flag=0;
  110. 110 //break;
  111. 111 }
  112. 112 else
  113. 113 {
  114. 114 if(mop[robot[num].y][robot[num].x]!=0)
  115. 115 {
  116. 116 k=mop[robot[num].y][robot[num].x];
  117. 117 flag=0;
  118. 118 }
  119. 119 else
  120. 120 mop[robot[num].y][robot[num].x]=num;
  121. 121 }
  122. 122 }
  123. 123 else
  124. 124 if(robot[num].id=='W')
  125. 125 {
  126. 126 mop[robot[num].y][robot[num].x]=0;
  127. 127 robot[num].x--;
  128. 128 if(robot[num].x<=0)
  129. 129 {
  130. 130 flag=0;
  131. 131 //break;
  132. 132 }
  133. 133 else
  134. 134 {
  135. 135 if(mop[robot[num].y][robot[num].x]!=0)
  136. 136 {
  137. 137 k=mop[robot[num].y][robot[num].x];
  138. 138 flag=0;
  139. 139 }
  140. 140 else
  141. 141 mop[robot[num].y][robot[num].x]=num;
  142. 142 }
  143. 143 }
  144. 144 }
  145. 145 else
  146. 146 if(dest=='L')//turn left
  147. 147 {
  148. 148 if(robot[num].id=='E')
  149. 149 robot[num].id='N';
  150. 150 else
  151. 151 if(robot[num].id=='W')
  152. 152 robot[num].id='S';
  153. 153 else
  154. 154 if(robot[num].id=='N')
  155. 155 robot[num].id='W';
  156. 156 else
  157. 157 if(robot[num].id=='S')
  158. 158 robot[num].id='E';
  159. 159 }
  160. 160 else
  161. 161 if(dest=='R')//turn right
  162. 162 {
  163. 163 if(robot[num].id=='E')
  164. 164 robot[num].id='S';
  165. 165 else
  166. 166 if(robot[num].id=='W')
  167. 167 robot[num].id='N';
  168. 168 else
  169. 169 if(robot[num].id=='N')
  170. 170 robot[num].id='E';
  171. 171 else
  172. 172 if(robot[num].id=='S')
  173. 173 robot[num].id='W';
  174. 174 }
  175. 175 }
  176. 176 }
  177. 177
  178. 178 }
  179. 179 if(flag==0)
  180. 180 {
  181. 181 if(k==0)
  182. 182 printf("Robot %d crashes into the wall\n",cur);
  183. 183 else
  184. 184 printf("Robot %d crashes into robot %d\n",cur,k);
  185. 185 }
  186. 186 else
  187. 187 printf("OK\n");
  188. 188 }
  189. 189 int main()
  190. 190 {
  191. 191 cin>>case1;
  192. 192 while(case1--)
  193. 193 {
  194. 194 sovle();
  195. 195 }
  196. 196 return 0;
  197. 197 }

坚持!!多刷题!!

转载于:https://www.cnblogs.com/sdau--codeants/p/3351807.html

发表评论

表情:
评论列表 (有 0 条评论,403人围观)

还没有评论,来说两句吧...

相关阅读