poj2632(Crashing Robots)

题目大意:

     一个仓库有N个机器人在移动,仓库的大小为A,B。会有M次操作,(E,W,S,N)分别代表东西南北,机器人在仓库里会有三种情况:1.正在移动的机器人i撞墙。2.正在移动的机器人i遇见机器人j。3.所有机器人没发生任何意外(OK)。根据操作,输出可能的结果。

数据分析

   4 //代表几组测试案例

   5 4 //代表仓库的大小 

   2 2  //代表有两个机器人进行两次操作

   1 1 E  //代表第一个机器人的位置和方向

   5 4 W //代表第二个机器人的位置和方向

   1 F 7   //代表编号为1的机器人向前走7次

   2 L 7  //代表编号为2的机器人向左转7次。 (R向右转)

解题思路:

    模拟即可,模拟机器人操作的每一步,在模拟的过程中判断机器人是否撞墙或者相遇,如果出现此状况就记录下来然后BREAK。最后输出结果。

    注意(模拟可能代码会很长,一定要细心,多做几组测试数据调试检验代码的正确性)。

代码:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cstdio>
  7 #include <string>
  8 #include <bitset>
  9 #include <vector>
 10 #include <queue>
 11 #include <stack>
 12 #include <cmath>
 13 #include <list>
 14 #include <map>
 15 #include <set>
 16 using namespace std;
 17 /***************************************/
 18 #define ll long long
 19 #define int64 __int64
 20 /***************************************/
 21 const int INF = 0x7f7f7f7f;
 22 const double eps = 1e-8;
 23 const double PIE=acos(-1.0);
 24 const int dx[]= {0,-1,0,1};
 25 const int dy[]= {1,0,-1,0};
 26 const int fx[]= {-1,-1,-1,0,0,1,1,1};
 27 const int fy[]= {-1,0,1,-1,1,-1,0,1};
 28 /***************************************/
 29 void openfile()
 30 {
 31     freopen("data.in","rb",stdin);
 32     freopen("data.out","wb",stdout);
 33 }
 34 /**********************华丽丽的分割线,以上为模板部分*****************/
 35 char fang[4]= {'E','N','W','S'};
 36 struct Node
 37 {
 38     int x,y;
 39     int c;
 40 } node[200];
 41 int main()
 42 {
 43     int cas;
 44     scanf("%d",&cas);
 45     while(cas--)
 46     {
 47         int A,B;
 48         int i,j;
 49         scanf("%d%d",&A,&B);
 50         int n,m;
 51         char c1;
 52         scanf("%d%d",&n,&m);
 53         for(i=1; i<=n; i++)
 54         {
 55             scanf("%d%d",&node[i].y,&node[i].x);
 56             getchar();
 57             scanf("%c",&c1);
 58             getchar();
 59             if (c1=='E')
 60                 node[i].c=0;
 61             if (c1=='N')
 62                 node[i].c=1;
 63             if (c1=='W')
 64                 node[i].c=2;
 65             if (c1=='S')
 66                 node[i].c=3;
 67         }
 68         int ce=0;
 69         int robot1,robot2,robot3;
 70         for(i=1; i<=m; i++)
 71         {
 72             int robot,sum;
 73             char action;
 74             scanf("%d",&robot);
 75             getchar();
 76             scanf("%c",&action);
 77             getchar();
 78             scanf("%d",&sum);
 79             if (ce)
 80                 continue;
 81             if (action=='L')
 82             {
 83                 for(j=0; j<sum; j++)
 84                 {
 85                     node[robot].c+=1;
 86                     if (node[robot].c==4)
 87                         node[robot].c=0;
 88                 }
 89             }
 90             if (action=='R')
 91             {
 92                 for(j=0; j<sum; j++)
 93                 {
 94                     node[robot].c-=1;
 95                     if (node[robot].c==-1)
 96                         node[robot].c=3;
 97                 }
 98             }
 99             if (action=='F')
100             {
101                 if (node[robot].c==0)
102                 {
103                     for(j=1; j<=sum; j++)
104                     {
105                         if (ce)
106                             break;
107                         node[robot].y+=1;
108                         if (node[robot].y>A)
109                         {
110                             ce=1;
111                             robot1=robot;
112                             //    printf("Robot %d crashes into the wall
",robot);
113                         }
114                         for(int k=1; k<=n; k++)
115                         {
116                             if (k==robot)
117                                 continue;
118                             if (node[k].x==node[robot].x&&node[k].y==node[robot].y)
119                             {
120                                 ce=2;
121                                 robot2=robot;
122                                 robot3=k;
123                                 //  printf("Robot %d crashes into robot %d
",robot,k);
124                             }
125                         }
126 
127                     }
128                 }
129                 if (node[robot].c==1)
130                 {
131                     for(j=1; j<=sum; j++)
132                     {
133                         if (ce)
134                             break;
135                         node[robot].x+=1;
136                         if (node[robot].x>B)
137                         {
138                             ce=1;
139                             robot1=robot;
140                             // printf("Robot %d crashes into the wall
",robot);
141                         }
142                         for(int k=1; k<=n; k++)
143                         {
144                             if (k==robot)
145                                 continue;
146                             if (node[k].x==node[robot].x&&node[k].y==node[robot].y)
147                             {
148                                 ce=2;
149                                 robot2=robot;
150                                 robot3=k;
151                                 //   printf("Robot %d crashes into robot %d
",robot,k);
152                             }
153                         }
154 
155                     }
156                 }
157                 if (node[robot].c==2)
158                 {
159                     for(j=1; j<=sum; j++)
160                     {
161                         if (ce)
162                             break;
163                         node[robot].y-=1;
164                         if (node[robot].y<1)
165                         {
166                             ce=1;
167                             robot1=robot;
168                             //  printf("Robot %d crashes into the wall
",robot);
169                         }
170                         for(int k=1; k<=n; k++)
171                         {
172                             if (k==robot)
173                                 continue;
174                             if (node[k].x==node[robot].x&&node[k].y==node[robot].y)
175                             {
176                                 ce=2;
177                                 robot2=robot;
178                                 robot3=k;
179                                 //  printf("Robot %d crashes into robot %d
",robot,k);
180                             }
181                         }
182 
183                     }
184                 }
185                 if (node[robot].c==3)
186                 {
187                     for(j=1; j<=sum; j++)
188                     {
189                         if (ce)
190                             break;
191                         node[robot].x-=1;
192                         if (node[robot].x<1)
193                         {
194                             ce=1;
195                             robot1=robot;
196                             // printf("Robot %d crashes into the wall
",robot);
197                         }
198                         for(int k=1; k<=n; k++)
199                         {
200                             if (k==robot)
201                                 continue;
202                             if (node[k].x==node[robot].x&&node[k].y==node[robot].y)
203                             {
204                                 ce=2;
205                                 robot2=robot;
206                                 robot3=k;
207                                 // printf("Robot %d crashes into robot %d
",robot,k);
208                             }
209                         }
210 
211                     }
212                 }
213             }
214         }
215         if (!ce)
216             printf("OK
");
217         else if (ce==1)
218             printf("Robot %d crashes into the wall
",robot1);
219         else if (ce==2)
220             printf("Robot %d crashes into robot %d
",robot2,robot3);
221     }
222     return 0;
223 }
View Code

    

屌丝终有逆袭日,*******。
原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/3757011.html