PKU 2643 Crashing Robots

http://acm.pku.edu.cn/JudgeOnline/problem?id=2632

 

模拟题,关键是要将题目要求读清楚,比如这道题,robots 得到每条指令后怎么执行等,

由于代码经常会很长,好的代码风格有助于找错

 

  
    
1 //2632 Accepted 204K 0MS C++ 1749B 2010-04-05 11:29:35
2  #include <stdio.h>
3 #include <string.h>
4  #define NL 110
5
6  struct Rob {
7 int a, b, dr; //注意方向
8  };
9 Rob rb[NL];
10  int map[NL][NL]; //将S-N方向调换,则map[b][a]存的是 (a, b)位置的机器人
11 int A, B, n, m;
12 int n0, rp;
13 int rbt[2];
14
15 int dir[4][2] = {
16 {0, -1}, // S
17 {-1, 0}, //W
18 {0, 1}, //N
19 {1, 0} //E
20 };
21
22 int getdr(char s0[2])
23 {
24 int dt;
25 switch(s0[0]) {
26 case 'S':
27 dt = 0;
28 break;
29 case 'W':
30 dt = 1;
31 break;
32 case 'N':
33 dt = 2;
34 break;
35 case 'E':
36 dt = 3;
37 break;
38 }
39 return dt;
40 }
41
42 int move(char s[2])
43 {
44 int a = rb[n0].a;
45 int b = rb[n0].b;
46 int dt = rb[n0].dr;
47 int rp0;
48
49 rp0 = rp;
50 //三种指令
51 while (a>=1 && a<=A
52 && b>=1 && b<=B
53 && rp0--) {
54 //左转,注意是--,因为方向调换了
55 if (s[0] == 'L') {
56 dt = (dt-1+4)%4;
57 }else if (s[0] == 'R') {
58 dt = (dt+1)%4;
59 }else {
60 a = a + dir[dt][0];
61 b = b + dir[dt][1];
62 if (a>=1 && a<=A
63 && b>=1 && b<=B) {
64 if (map[b][a]) {
65 rbt[0] = n0;
66 rbt[1] = map[b][a];
67 return 1;
68 }
69 }else {
70 rbt[0] = n0;
71 return 2;
72 }
73 }
74 }
75 rb[n0].dr = dt;
76 map[rb[n0].b][rb[n0].a] = 0;
77 rb[n0].a = a;
78 rb[n0].b = b;
79 map[b][a] = n0;
80 return 0;
81 }
82
83 int main()
84 {
85 int T;
86 int i;
87 char s[2];
88 int flg;
89
90 scanf("%d", &T);
91 while (T--) {
92 scanf("%d%d", &A, &B);
93 scanf("%d%d", &n, &m);
94 memset(rb, 0, sizeof(rb));
95 memset(map, 0, sizeof(map));
96
97 for (i=1; i<=n; i++) {
98 scanf("%d%d%s", &rb[i].a, &rb[i].b, s);
99 rb[i].dr = getdr(s);
100 map[rb[i].b][rb[i].a] = i;
101 }
102 flg = 0;
103 for (i=1; i<=m; i++) {
104 scanf("%d%s%d", &n0, s, &rp);
105 if (flg) continue;
106 flg = move(s);
107 }
108 if (!flg) printf("OK\n");
109 else if (flg == 1) {
110 printf("Robot %d crashes into robot %d\n", rbt[0], rbt[1]);
111 }else {
112 printf("Robot %d crashes into the wall\n", rbt[0]);
113 }
114
115 }
116 return 0;
117 }
118

 

 



你可能感兴趣的:(Crash)