-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2018 S3.cpp
195 lines (123 loc) · 5.67 KB
/
2018 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
/*
2018 S3 - RoboThieves
Difficulty: Medium
Topics: Graph Theory
So this problem is quite evidently a breadth first search problem since you have to traverse the entire factory starting from one point and you can only go to adjacent points up down left right
Tricky parts about this problem:
Cameras can only see empty space, and are stopped at a wall, they skip conveyors.
Conveyors. Conveyors can go in circles, so we must keep track of what we've already visited
*/
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <utility>
std::vector<std::string> factory; //Store layout of factory
int N, M;
//Check for a camera, return true if a camera can see the robot, note that the factor always has walls as its perimeter
bool seen (int y, int x){
//Check upwards from player, stop when a wall is hit
for (int i = y - 1; i > -1 && factory[i][x] != 'W'; i--){
if (factory[i][x] == 'C'){
return true;
}
}
//Check downwards
for (int i = y + 1; i < N && factory[i][x] != 'W'; i++){
if (factory[i][x] == 'C'){
return true;
}
}
//Check towards the left
for (int i = x - 1; i > -1 && factory[y][i] != 'W'; i--){
if (factory[y][i] == 'C'){
return true;
}
}
//Check towards the right
for (int i = x + 1; i < M && factory[y][i] != 'W'; i++){
if (factory[y][i] == 'C'){
return true;
}
}
return false;
}
int main(){
std::cin >> N >> M;
factory.resize(N);
//Collect input
for (int i = 0; i < N; i++){
std::cin >> factory[i];
}
std::pair<int, int> startPos; //Represents robot's starting position
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
if (factory[i][j] == 'S'){
startPos = std::make_pair(i, j);
}
}
}
std::vector<std::vector<int>> distances (N, std::vector<int> (M, -1)); //Storing distance to each place in the factory, default to -1 since it indicates impossible
std::vector<std::vector<bool>> alreadyVisited (N, std::vector<bool> (M, false)); //Keeping track of what has been visited
std::queue<std::pair<int, int>> q; //Using queue, preparing for Breadth First Search since we're trying to find the minimum distance
q.push(startPos); //Add start
alreadyVisited[startPos.first][startPos.second] = true; //Mark start as visited
int distance = 0; //Keep track of depth of breadth first search
//This is just for iterating over up, right, left, down, you'll understand once you read the loop
const int xShift[4] = {0, 1, 0, -1};
const int yShift[4] = {1, 0, -1, 0};
//You can spawn beside a camera
if (!seen(startPos.first, startPos.second)){
while (!q.empty()){
int size = q.size(); //To search for just this horizontal level of the graph
for (int i = 0; i < size; i++){
std::pair<int, int> currentPos = q.front();
q.pop();
distances[currentPos.first][currentPos.second] = distance; //Set distance
for (int j = 0; j < 4; j++){
//Check up right left down, here's where the shifts are used
std::pair<int, int> possibleLocation = std::make_pair(currentPos.first + yShift[j], currentPos.second + xShift[j]);
if (alreadyVisited[possibleLocation.first][possibleLocation.second]){
continue;
}
char letter = factory[possibleLocation.first][possibleLocation.second]; //Check letter
//If on a conveyor
while (!alreadyVisited[possibleLocation.first][possibleLocation.second] && letter != '.' && letter != 'W' && letter != 'C'){
alreadyVisited[possibleLocation.first][possibleLocation.second] = true;
if (letter == 'U'){
possibleLocation.first--;
}
else if (letter == 'D'){
possibleLocation.first++;
}
else if (letter == 'L'){
possibleLocation.second--;
}
else if (letter == 'R'){
possibleLocation.second++;
}
letter = factory[possibleLocation.first][possibleLocation.second];
}
if (alreadyVisited[possibleLocation.first][possibleLocation.second]){
continue;
}
alreadyVisited[possibleLocation.first][possibleLocation.second] = true; //Very important that you mark locations that have already been visited
//Add unexplored areas
if (letter == '.' && !seen(possibleLocation.first, possibleLocation.second)){
q.push(possibleLocation);
}
}
}
distance++; //Increase depth
}
}
//Print our answer
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
if (factory[i][j] == '.'){
std::cout << distances[i][j] << '\n';
}
}
}
return 0;
}