-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaze.cpp
400 lines (364 loc) · 13.2 KB
/
maze.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*************************
* maze.cpp
* editor: amo
*************************/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <maze.h>
#include <algorithm> // std::for_each
#include <iterator> // std::back_inserter
#
using namespace amo;
/* -------------------------------------------------------------------- */
/* my define macro */
/* -------------------------------------------------------------------- */
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define INIT_OUT INIT
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
// Allocating and initializing Class's static data member
Maze* Maze::instance = 0;
//std::stack<amo::Cell> Maze::path;
static std::vector<amo::Cell*> moves;
/* -------------------------------------------------------------------- */
/* implements */
/* -------------------------------------------------------------------- */
amo::Maze::~Maze() {
std::cout << "[Maze::~Maze()]:" << this << ", row:" << row << ", col:" << col << std::endl;
//delete Maze::instance;
}
amo::Maze::Maze(int n, int m): row(n), col(m) {
std::cout << "[Maze::Maze()]:" << this << ", row:" << row << ", col:" << col << std::endl;
for (int i=0; i<row; i++) {
std::cout << YELLOW << "[Maze::Maze()]: going to create elements of row:" << i << WHITE << std::endl;
*(mz+i) = new Cell[col];
//*(mz+i) = (Cell*) malloc(sizeof(Cell)*col);
for (int j=0; j<col; j++) {
mz[i][j] = Cell(i, j);
if (i == 1 && j == 3) mz[i][j].status = WALL;
if (i == 2 && j == 0) mz[i][j].status = WALL;
if (i == 2 && j == 1) mz[i][j].status = WALL;
if (i == 3 && j == 0) mz[i][j].status = WALL;
}
}
}
Maze& amo::Maze::getInstance(int n, int m) {
if (instance == nullptr) {
std::cout << "[Maze::getInstance()]: going to 'instance = new Maze()'" << std::endl;
instance = new Maze(n, m);
}
return *instance;
}
#if 0
void collect_move(std::vector<amo::Cell*>& vector, int count, ...) {
va_list ap;
va_start(ap, count);
amo::Cell* e;
for (int i=0; i<count; i=i++) {
e = va_arg(ap, amo::Cell*);
vector.push_back(e);
}
va_end(ap);
}
#else
void collect_move(std::vector<amo::Cell*>& vector, amo::Cell* e) {
vector.push_back(e);
}
#endif
void erase_move(std::vector<amo::Cell*>& vector, amo::Cell* e) {
for (std::vector<amo::Cell*>::iterator it=vector.begin(); it!=vector.end(); it++) {
if (**(it) == *e) {
std::cout << "[Maze::getInstance()]: going to erase cell:" << *e << " from moves at " << std::distance(vector.begin(), it) << std::endl;
vector.erase(it);
}
}
vector.push_back(e);
}
inline bool is_move(std::vector<amo::Cell*>& vector, int i, int j) {
for (std::vector<amo::Cell*>::iterator it=vector.begin(); it!=vector.end(); it++)
if (i==(*it)->x && j==(*it)->y) return true;
return false;
}
void amo::Maze::traverse(std::vector<amo::Cell*>& vector) {
std::cout << BLUE <<"[Maze::traverse()]: --- Traverse begin ---" << WHITE << std::endl;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (is_move(vector, i, j)) {
if (mz[i][j].status == WALL) std::cout << RED << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
else if (mz[i][j].status == BACK) std::cout << CYAN << "["<< i <<"][" << j << "]" << mz[i][j] << WHITE;
else if (mz[i][j].status == ROUTE) std::cout << YELLOW << "["<< i <<"][" << j << "]" << mz[i][j] << WHITE;
else std::cout << GREEN << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
}
else {
if (mz[i][j].status == WALL) std::cout << MAGENTA << "["<< i <<"][" << j << "]" << mz[i][j] << WHITE;
else std::cout << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
}
if (j==col-1) std::cout << "" << std::endl;
}
}
std::cout << BLUE << "[Maze::traverse()]: --- Traverse end ---" << WHITE << std::endl;
}
void amo::Maze::traverse(std::stack<amo::Cell*>& stack) {
std::cout << BLUE <<"[Maze::traverse()]: --- Traverse begin ---" << WHITE << std::endl;
std::vector<amo::Cell*> vector;
while(!stack.empty()) {
vector.push_back(stack.top());
stack.pop();
}
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (is_move(vector, i, j)) {
if (mz[i][j].status == WALL) std::cout << RED << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
else std::cout << GREEN << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
}
else {
if (mz[i][j].status == WALL) std::cout << MAGENTA << "["<< i <<"][" << j << "]" << mz[i][j] << WHITE;
else std::cout << "[" << i <<"][" << j << "]" << mz[i][j] << WHITE;
}
if (j==col-1) std::cout << "" << std::endl;
}
}
std::cout << BLUE << "[Maze::traverse()]: --- Traverse end ---" << WHITE << std::endl;
}
bool amo::Maze::labyrinth(int sx, int sy, int tx, int ty) {
Cell* s = new Cell(sx, sy);
Cell* t = new Cell(tx, ty);
Cell* before;
int repeat = 0;
Cell* probing = s;
std::stack<amo::Cell*> path;
std::list<amo::Cell*> tmp;
do {
if (!(64>repeat++)){ //retry limit
std::cout << RED << "[Maze::labyrinth()]: No solution" << WHITE << std::endl;
break;
}
if (!moves.empty() && moves.size()>1) { //resets status of the one before current one if it's recorded as route to move like the game Snake
before = moves.at(moves.size()-2);
if (before->status == ROUTE) before->status = AVAL; //Route(Before)|Route(Current)|Aval(Next) -> Aval(Before)|Route(Current)|Aval(Next)
}
probe(&probing); //probes and decides the next direction
if (probing == NULL) break; //error
std::cout << GREEN << "[Maze::labyrinth()]: tried:" << repeat << " times, probing:[" << probing->x << "][" << probing->y << "]:" << *probing << WHITE << std::endl;
collect_move(moves, probing);
if (probing->x == t->x && probing->y == t->y) { //checks if arrived target or goes ahead to probe
std::cout << GREEN << "[Maze::labyrinth()]: Got solution" << WHITE << std::endl;
std::copy(moves.begin(), moves.end(), back_inserter(tmp));
tmp.unique();
for (std::list<Cell*>::iterator it=tmp.begin(); it!=tmp.end(); it++) path.push(*it);
traverse(path);
break;
}
traverse(moves);
} while(true);
//collect_move(moves, s);
//collect_move(moves, t);
//traverse(moves);
return false;
}
/**
* manipulation of cells
*/
int amo::Maze::next_available(Cell& c) {
int out = c.out;
int i = c.x;
int j = c.y;
amo::Cell* next;
int dir = c.out;
while (DEAD>dir++) {
switch (dir) {
case EAST:
if ((j+1)>=col) {
std::cout << "[Maze::next_available()]: NOT EAST at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
next = &mz[i][j+1];
if (next->status == AVAL) {
std::cout << "[Maze::next_available()]: NEXT EAST from [" << i << "][" << j << "]"
<< " onto [" << i << "][" << j+1 << "]" << WHITE << std::endl;
return dir;
} else {
std::cout << "[Maze::next_available()]: NOT AVAL EAST at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
case SOUTH:
if ((i+1)>=row) {
std::cout << "[Maze::next_available()]: NOT SOUTH at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
next = &mz[i+1][j];
if (next->status == AVAL) {
std::cout << "[Maze::next_available()]: NEXT SOUTH from [" << i << "][" << j << "]"
<< " onto [" << i+1 << "][" << j << "]" << WHITE << std::endl;
return dir;
} else {
std::cout << "[Maze::next_available()]: NOT AVAL SOUTH at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
case WEST:
if ((j-1)<0) {
std::cout << "[Maze::next_available()]: NOT WEST at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
next = &mz[i][j-1];
if (next->status == AVAL) {
std::cout << "[Maze::next_available()]: NEXT WEST from [" << i << "][" << j << "]"
<< " onto [" << i << "][" << j-1 << "]" << WHITE << std::endl;
return dir;
} else {
std::cout << "[Maze::next_available()]: NOT AVAL WEST at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
case NORTH:
if ((i-1)<0) {
std::cout << "[Maze::next_available()]: NOT NORTH at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
next = &mz[i-1][j];
if (next->status == AVAL) {
std::cout << "[Maze::next_available()]: NEXT NORTH from [" << i << "][" << j << "]"
<< " onto [" << i-1 << "][" << j << "]" << WHITE << std::endl;
return dir;
} else {
std::cout << "[Maze::next_available()]: NOT AVAL NORTH at [" << i << "][" << j << "]" << WHITE << std::endl;
break;
}
}
}
std::cout << "[Maze::next_available()]: returns DEAD:" << dir << " at [" << i << "][" << j << "]" << WHITE << std::endl;
return dir-1;//DEAD
}
void amo::Maze::probe(amo::Cell** c) {
switch ((**c).out) {
case INIT:
std::cout << YELLOW << "[Maze::probe()]: MEET INIT at [" << (**c).x << "][" << (**c).y << "]" << WHITE << std::endl;
(**c).out = next_available(**c);
probe(c); //recurs to probe a certain position
return;
case EAST:
std::cout << YELLOW << "[Maze::probe()]: GO EAST from [" << (**c).x << "][" << (**c).y << "]"
<< " onto [" << (**c).x << "][" << (**c).y+1 << "]" << WHITE << std::endl;
(**c).status = ROUTE; //marks old moves for avoiding to go out of then go back to the same position
*c = &mz[(**c).x][(**c).y+1]; //probes
(**c).in = WEST;
(**c).out = next_available(**c);
return;
case SOUTH:
std::cout << YELLOW << "[Maze::probe()]: GO SOUTH from [" << (**c).x << "][" << (**c).y << "]"
<< " onto [" << (**c).x+1 << "][" << (**c).y << "]" << WHITE << std::endl;
(**c).status = ROUTE;
*c = &mz[(**c).x+1][(**c).y];
(**c).in = NORTH;
(**c).out = next_available(**c);
return;
case WEST:
std::cout << YELLOW << "[Maze::probe()]: GO WEST from [" << (**c).x << "][" << (**c).y << "]"
<< " onto [" << (**c).x << "][" << (**c).y-1 << "]" << WHITE << std::endl;
(**c).status = ROUTE;
*c = &mz[(**c).x][(**c).y-1];
(**c).in = EAST;
(**c).out = next_available(**c);
return;
case NORTH:
std::cout << YELLOW << "[Maze::probe()]: GO NORTH from [" << (**c).x << "][" << (**c).y << "]"
<< " onto [" << (**c).x-1 << "][" << (**c).y << "]" << WHITE << std::endl;
(**c).status = ROUTE;
*c = &mz[(**c).x-1][(**c).y];
(**c).in = SOUTH;
(**c).out = next_available(**c);
return;
case DEAD:
std::cout << YELLOW << "[Maze::probe()]: MEET DEAD at [" << (**c).x << "][" << (**c).y << "]" << WHITE << std::endl;
(**c).status = BACK;
(**c).out = (**c).in;
(**c).in = INIT;
*c = pry(**c); // backtracks
if (*c == NULL) return; //error
(**c).out = next_available(**c); //decides another different move from the backtracking position
(**c).in = INIT;
return;
default:
std::cout << YELLOW << "[Maze::probe()]: ERROR at [" << (**c).x << "][" << (**c).y << "]" << WHITE << std::endl;
*c = NULL;
return;
}
}
amo::Cell* amo::Maze::pry(amo::Cell& c) {
amo::Cell* next = NULL;
if (c.out >= DEAD) {
std::cout << "[Maze::pry()]: [" << c.x << "][" << c.y << "] is DEAD:" << c.out << WHITE << std::endl;
return NULL;
}
switch (c.out) {
case INIT:
std::cout << "[Maze::pry()]: INIT" << WHITE << std::endl;
next = NULL;
break;
case EAST:
if ((c.y+1)>=col) {
std::cout << "[Maze::pry()]: Beyond EAST" << WHITE << std::endl;
return NULL;
}
else {
std::cout << "[Maze::pry()]: EAST" << WHITE << std::endl;
next = &mz[c.x][c.y+1];
}
break;
case SOUTH:
if ((c.x+1)>=row) {
std::cout << "[Maze::pry()]: Beyond SOUTH" << WHITE << std::endl;
return NULL;
}
else {
std::cout << "[Maze::pry()]: SOUTH" << WHITE << std::endl;
next = &mz[c.x+1][c.y];
}
break;
case WEST:
if ((c.y-1)<0) {
std::cout << "[Maze::pry()]: Beyond WEST" << WHITE << std::endl;
return NULL;
}
else {
std::cout << "[Maze::pry()]: WEST" << WHITE << std::endl;
next = &mz[c.x][c.y-1];
}
break;
case NORTH:
if ((c.x-1)<0) {
std::cout << "[Maze::pry()]: Beyond NORTH" << WHITE << std::endl;
return NULL;
}
else {
std::cout << "[Maze::pry()]: NORTH" << WHITE << std::endl;
next = &mz[c.x-1][c.y];
}
break;
case DEAD:
std::cout << "[Maze::pry()]: DEAD" << WHITE << std::endl;
return NULL;
default:
std::cout << "[Maze::pry()]: ERROR" << WHITE << std::endl;
return NULL;
}
return next;
}