POJ 2632 Crashing Robots

模拟机器人走的序列,用一个数组标记当前每个位置是否有机器人以及机器人的编号就行

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;

struct robot{
int x;
int y;
int dir;
}robots[101];

int main(){
int ca;
int num, step;
int A, B, N, M;
char tmp;
int arr[101][101];
bool flag;
scanf("%d", &ca);
while(ca--){
flag = true;
memset(arr, 0, sizeof(arr));
scanf("%d%d%d%d", &A, &B, &N, &M);
for(int i = 1; i <= N; i++){
scanf("%d%d", &robots[i].x, &robots[i].y);
arr[robots[i].x][robots[i].y] = i;
getchar();
scanf("%c", &tmp);
if(tmp == 'W')
robots[i].dir = 0;
else if(tmp == 'N')
robots[i].dir = 1;
else if(tmp == 'E')
robots[i].dir = 2;
else if(tmp == 'S')
robots[i].dir = 3;
}
for(int i = 0; i < M; i++){
scanf("%d", &num);
getchar();
scanf("%c", &tmp);
scanf("%d", &step);
if(flag == false)
continue;
if(tmp == 'F'){
if(robots[num].dir == 0){
for(int j = 1; j <= step && robots[num].x-j > 0; j++){
if(arr[robots[num].x - j][robots[num].y] != 0){
printf("Robot %d crashes into robot %d\n", num, arr[robots[num].x-j][robots[num].y]);
flag = false;
break;
}
}
if(flag == false)
continue;
if(robots[num].x-step > 0){
arr[robots[num].x][robots[num].y] = 0;
robots[num].x -= step;
arr[robots[num].x][robots[num].y] = num;
}else{
printf("Robot %d crashes into the wall\n", num);
flag = false;
}
}else if(robots[num].dir == 1){
for(int j = 1; j <= step && robots[num].y+j <= B; j++){
if(arr[robots[num].x][robots[num].y+j] != 0){
printf("Robot %d crashes into robot %d\n", num, arr[robots[num].x][robots[num].y+j]);
flag = false;
break;
}
}
if(flag == false)
continue;
if(robots[num].y+step <= B){
arr[robots[num].x][robots[num].y] = 0;
robots[num].y += step;
arr[robots[num].x][robots[num].y] = num;
}else{
printf("Robot %d crashes into the wall\n", num);
flag = false;
}
}else if(robots[num].dir == 2){
for(int j = 1; j <= step && robots[num].x+j <= A; j++){
if(arr[robots[num].x + j][robots[num].y] != 0){
printf("Robot %d crashes into robot %d\n", num, arr[robots[num].x+j][robots[num].y]);
flag = false;
break;
}
}
if(flag == false)
continue;
if(robots[num].x+step <= A){
arr[robots[num].x][robots[num].y] = 0;
robots[num].x += step;
arr[robots[num].x][robots[num].y] = num;
}else{
printf("Robot %d crashes into the wall\n", num);
flag = false;
}
}else if(robots[num].dir == 3){
for(int j = 1; j <= step && robots[num].y-j > 0; j++){
if(arr[robots[num].x][robots[num].y-j] != 0){
printf("Robot %d crashes into robot %d\n", num, arr[robots[num].x][robots[num].y-j]);
flag = false;
break;
}
}
if(flag == false)
continue;
if(robots[num].y-step > 0){
arr[robots[num].x][robots[num].y] = 0;
robots[num].y -= step;
arr[robots[num].x][robots[num].y] = num;
}else{
printf("Robot %d crashes into the wall\n", num);
flag = false;
}
}
}else if(tmp == 'L'){
robots[num].dir = (robots[num].dir - (step%4) + 4) % 4;
}else if(tmp == 'R'){
robots[num].dir = (robots[num].dir + (step%4) + 4) % 4;
}
}
if(flag == true)
printf("OK\n");
}

return 0;
}