-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.cc
322 lines (272 loc) · 6.91 KB
/
maze.cc
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <iostream>
#include <iomanip>
#include <vector>
#include <deque>
#include <cassert>
#include <functional>
#include <algorithm>
#include <ostream>
#include <sstream>
// field coordinates struct
struct coords {
int m,n;
inline coords up() const noexcept {
return {m-1,n};
}
inline coords down() const noexcept {
return {m+1,n};
}
inline coords left() const noexcept {
return {m,n-1};
}
inline coords right() const noexcept {
return {m,n+1};
}
inline std::vector<coords> neighbors() const noexcept {
return {up(),down(),left(),right()};
}
};
// 2d vector class
class vec2 {
public:
const int M, N;
// ctor
inline vec2(): vec2(0,0) {}
inline vec2(const int M_, const int N_, const int init=0):
M(M_), N(N_), data(M*N,init) {}
inline vec2(const int M_, std::vector<int> data_):
M(M_), N(M_ ? data_.size()/M: 0), data(std::move(data_)) {
assert(data_.size()%M == 0);
}
// access
inline int operator[](const coords& pos) const noexcept {
return data[pos.m*N+pos.n];
}
inline int& operator[](const coords& pos) noexcept {
return data[pos.m*N+pos.n];
}
// query
inline bool empty() const noexcept {
return data.empty();
}
inline bool valid(const coords& pos) const noexcept {
return pos.m>=0 && pos.m<M && pos.n>=0 && pos.n<N;
}
private:
std::vector<int> data;
};
// function to find the number of steps map for a maze
vec2 get_steps_map(const vec2& maze, const coords& origin) {
// check edge cases
if (maze.empty() || !maze.valid(origin))
return vec2();
// prepare result
vec2 res(maze.M,maze.N,-1);
res[origin] = 0;
// prepare current fields queue
std::deque<coords> current_fields;
current_fields.push_back(origin);
// incrementally fill up the result with step numbers
for (int current_step_index=1 ; !current_fields.empty(); ++current_step_index) {
// check and fill in neighbors of current fields
for (const auto& field: current_fields) {
for (const auto& nn: field.neighbors()) {
if (maze.valid(nn) && maze[nn]!=1 && res[nn]==-1) {
res[nn] = current_step_index;
current_fields.push_back(nn);
}
}
current_fields.pop_front();
}
}
return res;
}
std::vector<std::vector<coords>> find_shortest_paths(
const vec2& map, const coords& finish) noexcept {
// We start at the finish and go to the origin,
// i.e. the only field with 0 steps. We will move only into fields
// with a step count one below the current one, if there are multiple
// such fields we split and recursively follow all of them to find
// all the shortest paths across our maze from the finish back to the origin.
// Then we reverse the order to find all the shortest paths to the finish.
if (map.empty() || map[finish]==-1)
return {};
std::vector<std::vector<coords>> res;
std::function<void(std::vector<coords>)> recv;
recv = [&recv,&map,&res](std::vector<coords> path) -> void {
while (map[path.back()]) {
coords new_pos = {-1,-1};
for (const auto& nn: path.back().neighbors()) {
if (map.valid(nn) && map[nn]==map[path.back()]-1) {
if (new_pos.m == -1) {
new_pos = nn;
} else {
auto new_path = path;
new_path.push_back(nn);
recv(std::move(new_path));
}
}
}
path.push_back(new_pos);
}
res.push_back(std::move(path));
};
// call recursive lambda
recv({finish});
// reverse all the paths
for (auto& path: res)
std::reverse(res.begin(),res.end());
return res;
}
void print_maze(const vec2& maze, std::ostream& os=std::cout) noexcept {
coords pos;
for (pos.m=0; pos.m!=maze.M; ++pos.m) {
os << "|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
os << "-----|";
os << "\n|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
os << " " << (maze[pos]==1 ? "X":
maze[pos]==2 ? "S":
maze[pos]==3 ? "E": " ") << " |";
os << "\n";
}
os << "|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
os << "-----|";
os << "\n";
}
void print_steps_map(const vec2& maze, const vec2& map, std::ostream& os=std::cout) noexcept {
coords pos;
for (pos.m=0; pos.m!=maze.M; ++pos.m) {
os << "|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
os << "-----|";
os << "\n|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
if (maze[pos]==1)
os << " X |";
else
os << std::setw(3) << map[pos] << " |";
os << "\n";
}
os << "|";
for (pos.n=0; pos.n!=maze.N; ++pos.n)
os << "-----|";
os << "\n";
}
void print_path(const vec2& maze, const std::vector<coords>& path, std::ostream& os=std::cout) noexcept {
// print maze into a string
std::stringstream sstr;
print_maze(maze,sstr);
std::string str = sstr.str();
const int k = 6*maze.N+5;
const int s = 12*maze.N+4;
// mapping lambda
const auto l = [k,s](const coords& pos) -> int {
return k + s*pos.m + 6*pos.n;
};
// draw nodes
for (const auto& pos: path)
str[l(pos)] = 'o';
// draw connections
if (path.size()>=2) {
for (int i=1; i!=path.size(); ++i) {
const int dm = path[i].m - path[i-1].m;
if (dm > 0) {
for (int j=l(path[i-1])+s/2, e=l(path[i]); j!=e; j+=s/2)
str[j] = '|';
}
if (dm < 0) {
for (int j=l(path[i])+s/2, e=l(path[i-1]); j!=e; j+=s/2)
str[j] = '|';
}
const int dn = path[i].n - path[i-1].n;
if (dn > 0) {
for (int j=l(path[i-1])+1, e=l(path[i]); j!=e; ++j)
str[j] = '-';
}
if (dn < 0) {
for (int j=l(path[i])+1, e=l(path[i-1]); j!=e; ++j)
str[j] = '-';
}
}
}
os << str;
}
int main() {
// maze construction guide:
// 0: accessible field
// 1: inaccessible field
// 2: start
// 3: finish
/*
vec2 maze(4,
{2,0,0,1,
0,0,0,0,
0,0,1,0,
1,0,0,3});
*/
/*
vec2 maze(8,
{0,1,0,0,0,0,0,0,
0,1,2,1,0,1,1,0,
0,1,0,1,0,0,0,0,
0,0,0,0,0,0,1,0,
1,1,0,1,0,0,0,0,
1,0,0,0,0,1,0,0,
1,1,0,1,0,0,3,0,
0,0,0,1,0,0,0,0});
*/
/*
vec2 maze(9,
{2,1,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,0,
1,0,1,0,1,0,1,0,1,
0,0,0,0,0,0,0,0,0,
0,1,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,0,
1,0,1,0,1,0,1,0,1,
0,0,0,0,0,0,0,0,0,
0,1,0,1,0,1,0,1,3});
*/
vec2 maze(7,
{0,0,0,0,0,0,
0,0,0,0,1,0,
0,0,1,1,1,0,
3,0,1,2,0,0,
0,0,1,1,1,0,
0,0,0,0,1,0,
0,0,0,0,0,0});
coords start;
for (start.m=0; start.m!=maze.M; ++start.m) {
for (start.n=0; start.n!=maze.N; ++start.n) {
if (maze[start] == 2)
goto lbl1; // fuck it
}
}
lbl1:
coords finish;
for (finish.m=0; finish.m!=maze.M; ++finish.m) {
for (finish.n=0; finish.n!=maze.N; ++finish.n) {
if (maze[finish] == 3)
goto lbl2; // fuck it
}
}
lbl2:
const auto steps_map = get_steps_map(maze,start);
const auto paths = find_shortest_paths(steps_map,finish);
std::cout << "maze:\n";
print_maze(maze);
std::cout << "\nstep map:\n";
print_steps_map(maze,steps_map);
std::cout << "\npaths:\n";
for (const auto& path: paths) {
print_path(maze,path);
std::cout << "\n";
}
std::cout << "\nshortest path length: " << steps_map[finish];
std::cout << "\nnumber of shortest paths: " << paths.size() << "\n";
std::cout << "\nThank you and good night\n";
return 0;
}