POJ-2632 Crashing Robots模拟

题目链接:

https://vjudge.net/problem/POJ-2632

题目大意:

在一个a×b的仓库里有n个机器人,编号为1到n。现在给出每一个机器人的坐标和它所面朝的方向,以及m条指令,每条指令由三部分组成:整数num代表该条指令调用的机器人的编号;字符act表示操作:其中L表示原地向左转90°,R表示原地向右转90°,F表示向前走一步;整数rep表示执行该条指令的次数。已知,当两个机器人坐标相同时他们会相撞,某一个机器人走出仓库也会撞到墙,问你能否安全执行这m条指令,如果能则输出“OK”;否则输出中断原因(哪两个机器人相撞,或是哪个机器人撞到墙了)。

思路:

注意细节,直接模拟

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 using namespace std;
10 typedef long long ll;
11 const int maxn = 1e2 + 10;
12 const int INF = 1 << 30;
13 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
14 int T, n, m;
15 struct node
16 {
17     int x, y, dir;
18 };
19 node a[maxn];
20 map<char, int>M;
21 int Map[200][200];
22 int main()
23 {
24     cin >> T;
25     int c1, c2;
26     M['E'] = 0;
27     M['N'] = 1;
28     M['W'] = 2;
29     M['S'] = 3;
30     while(T--)
31     {
32         cin >> n >> m;
33         cin >> c1 >> c2;
34         char c;
35         memset(Map, 0, sizeof(Map));
36         memset(a, 0, sizeof(a));
37         for(int i = 1; i <= c1; i++)
38         {
39             cin >> a[i].x >> a[i].y >> c;
40             a[i].dir = M[c];
41             Map[a[i].x][a[i].y] = i;
42         }
43         int flag = 0, ansid1, ansid2;
44         for(int i = 1; i <= c2; i++)
45         {
46             int id, tot;
47             cin >> id >> c >> tot;
48             if(flag)continue;
49             if(c == 'L')
50             {
51                 a[id].dir += tot;
52                 a[id].dir %= 4;
53             }
54             else if(c == 'R')
55             {
56                 a[id].dir -= tot;
57                 a[id].dir = ((a[id].dir % 4) + 4) % 4;
58             }
59             else if(c == 'F')
60             {
61                 Map[a[id].x][a[id].y] = 0;
62                 for(int i = 1; i <= tot; i++)
63                 {
64                     //cout<<a[id].x<<" "<<a[id].y<<" "<<a[id].dir<<endl;
65                     a[id].x += dir[a[id].dir][0];
66                     a[id].y += dir[a[id].dir][1];
67 
68                     if(a[id].x <= 0 || a[id].x > n || a[id].y <= 0 || a[id].y > m)
69                     {
70                         flag = 1;
71                         ansid1 = id;
72                         break;
73                     }
74                     else if(Map[a[id].x][a[id].y])
75                     {
76                         flag = 2;
77                         ansid1 = id;
78                         ansid2 = Map[a[id].x][a[id].y];
79                     }
80                     if(flag)break;
81                 }
82                 Map[a[id].x][a[id].y] = id;
83             }
84         }
85         if(!flag)cout<<"OK"<<endl;
86         else if(flag == 1)
87             cout<<"Robot "<<ansid1<<" crashes into the wall"<<endl;
88         else cout<<"Robot "<<ansid1<<" crashes into robot "<<ansid2<<endl;
89     }
90     return 0;
91 }
原文地址:https://www.cnblogs.com/fzl194/p/8718772.html