POJ 2632 Crashing Robots

终于可以说这是道比较简单的模拟题了。

虽然做了3个多小时,但是自己比较满意。加油,以后会越来越快的。很享受思路清晰地编程实现。还有,C下面的调试我算是会用了。

View Code
  1 #include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 #include<cstdio>
5 using namespace std;
6
7 #define ONLINE
8
9 struct Robot
10 {
11 int x;
12 int y;
13 int d; //direction:N,W,S,E---0,1,2,3
14 }robots[101];
15
16 struct Instruction
17 {
18 int robotId;
19 char action;
20 int repeat;
21 }instructions[101];
22
23 int column,row;
24 int numOfRobot,numOfInstruction;
25
26 int direction[128];
27
28 void online()
29 {
30 #ifdef ONLINE
31 #else
32 freopen("D:\\t1.txt","r",stdin);
33 freopen("D:\\t2.txt","w",stdout);
34 #endif
35 }
36
37 bool estimate(int m,int n,int i)
38 {
39 for(int j=1;j<=numOfRobot;j++)
40 {
41 if(i!=j)
42 {
43 if(robots[j].x==m&&robots[j].y==n)
44 {
45 cout<<"Robot "<<i<<" crashes into robot "<<j<<endl;
46 return true;
47 }
48 }
49 }
50 if(m<1||m>column||n<1||n>row)
51 {
52 cout<<"Robot "<<i<<" crashes into the wall"<<endl;
53 return true;
54 }
55 return false;
56 }
57
58 void imitate()
59 {
60 for(int j=1;j<=numOfInstruction;++j)
61 {
62 if(instructions[j].action=='L')
63 robots[instructions[j].robotId].d=(robots[instructions[j].robotId].d+instructions[j].repeat)%4;
64 else if(instructions[j].action=='R')
65 robots[instructions[j].robotId].d=(robots[instructions[j].robotId].d+3*instructions[j].repeat)%4;
66 else if(instructions[j].action=='F')
67 {
68 while(instructions[j].repeat)
69 {
70 if(robots[instructions[j].robotId].d==0)
71 robots[instructions[j].robotId].y++;
72 else if(robots[instructions[j].robotId].d==1)
73 robots[instructions[j].robotId].x--;
74 else if(robots[instructions[j].robotId].d==2)
75 robots[instructions[j].robotId].y--;
76 else if(robots[instructions[j].robotId].d==3)
77 robots[instructions[j].robotId].x++;
78 if(estimate(robots[instructions[j].robotId].x,robots[instructions[j].robotId].y,instructions[j].robotId))
79 return;
80 instructions[j].repeat--;
81 }
82 }
83 }
84 cout<<"OK"<<endl;
85 }
86
87 void init()
88 {
89 memset(robots,0,sizeof(robots));
90 memset(instructions,0,sizeof(instructions));
91 memset(direction,-1,sizeof(direction));
92
93 direction[int('N')]=0;
94 direction[int('W')]=1;
95 direction[int('S')]=2;
96 direction[int('E')]=3;
97
98
99 int cases;
100 cin>>cases;
101 while(cases--)
102 {
103 cin>>column>>row;
104 cin>>numOfRobot>>numOfInstruction;
105 for(int i=1;i<=numOfRobot;++i)
106 {
107 int xx,yy;
108 char dd;
109 cin>>xx>>yy>>dd;
110 robots[i].x=xx;
111 robots[i].y=yy;
112 robots[i].d=direction[int(dd)];
113 }
114 for(int i=1;i<=numOfInstruction;++i)
115 {
116 int ii,jj;
117 char aa;
118 cin>>ii>>aa>>jj;
119 instructions[i].robotId=ii;
120 instructions[i].action=aa;
121 instructions[i].repeat=jj;
122 }
123 imitate();
124 }
125 }
126
127
128
129 int main()
130 {
131 online();
132 init();
133 return 0;
134 }

原文地址:https://www.cnblogs.com/YipWingTim/p/2219575.html