-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2002 J5-S3.cpp
197 lines (132 loc) · 5.08 KB
/
2002 J5-S3.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
2002 J5/S3 - Blindfold
Difficulty: Easy or Hard depending on what judge you're using
For the CCC data, m can be no larger than 50, so this question is very easy to brute force. For DMOJ added test cases where m = 30 000, the question becomes extremely difficult
CCC - 15/15
DMOJ 100/150
General idea, try every position in the backyard that is not a cell, then try with all 4 starting directions and simulate Colin's movement.
*/
#include <iostream>
#include <vector>
#include <string>
int main(){
int r, c;
std::cin >> r >> c;
std::vector<std::string> backyard (r); //Store backyard
//Fill in backyard
for (int i = 0; i < r; i++){
std::cin >> backyard[i];
}
int m;
std::cin >> m;
std::string instructions; //Storing instructions as string
for (int i = 0; i < m; i++){
char instruction;
std::cin >> instruction;
instructions += instruction;
}
std::vector<char> directions {'N', 'S', 'E', 'W'};
int colinx, coliny; //colinx is Colin's x position, coliny is Colin's y position
//For every row
for (int i = 0; i < r; i++){
coliny = i;
//for every column
for (int j = 0; j < c; j++){
colinx = j;
//If obstacle skip
if (backyard[coliny][colinx] == 'X'){
continue;
}
//For all 4 starting directions
for (char d: directions){
char direction = d;
int possible = 0; //Determine if it's possible to perform this set of instructions, eg possible = 0 if we walk out of bounds or hit an obstacle
//For each instruction
for (int k = 0; k < instructions.length(); k++){
//Forwards
if (instructions[k] == 'F'){
possible++;
//North
if (direction == 'N'){
coliny -= 1;
//If out of range or obstacle is hit
if (coliny < 0 || backyard[coliny][colinx] == 'X'){
possible = 0;
break;
}
}
//South
else if (direction == 'S'){
coliny += 1;
//If out of range or obstacle is hit
if (coliny == r || backyard[coliny][colinx] == 'X'){
possible = 0;
break;
}
}
//West
else if (direction == 'W'){
colinx -= 1;
//If out of range or obstacle is hit
if (colinx < 0 || backyard[coliny][colinx] == 'X'){
possible = 0;
break;
}
}
//East
else if (direction == 'E'){
colinx += 1;
//If out of range or obstacle is hit
if (colinx == c || backyard[coliny][colinx] == 'X'){
possible = 0;
break;
}
}
}
//Swap directions
else if (instructions[k] == 'L'){
if (direction == 'N'){
direction = 'W';
}
else if (direction == 'W'){
direction = 'S';
}
else if (direction == 'S'){
direction = 'E';
}
else{
direction = 'N';
}
}
//Swap directions
else{
if (direction == 'N'){
direction = 'E';
}
else if (direction == 'E'){
direction = 'S';
}
else if (direction == 'S'){
direction = 'W';
}
else{
direction = 'N';
}
}
}
//If instructions are possible, mark as a final position
if (possible > 0){
backyard[coliny][colinx] = '*';
}
//Reset coliny and colinx for the next possible starting direction
coliny = i;
colinx = j;
}
}
}
//Output new backyard
for (std::string line: backyard){
std::cout << line << '\n';
}
return 0;
}