Repository navigation
Expand file tree
/
Copy pathMaze.c
More file actions
389 lines (332 loc) · 11.2 KB
/
Copy pathMaze.c
File metadata and controls
389 lines (332 loc) · 11.2 KB
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
#include <assert.h> // assert()
#include <stdio.h> // printf() and snprintf()
#include <stdlib.h> // malloc() and free()
#include <string.h> // memset()
#include <wchar.h>
#include "Stack.h"
#define ROWS 10
#define COLS 10
// The state of a position in the maze:
// NOT_VISITED -> TO_RIGHT -> TO_DOWN -> TO_LEFT -> TO_UP -> FINISHED
typedef enum {
// This position has not been visited yet
NOT_VISITED = 0,
// go to visit the adjacent rightward position
TO_RIGHT = 1,
// go to visit the adjacent downward position
TO_DOWN = 2,
// go to visit the adjacent leftward position
TO_LEFT = 4,
// go to visit the adjacent upward position
TO_UP = 8,
// We have explored all four directions of the current position,
FINISHED = 0x0F,
} PositionState;
// Command line
// see https://en.wikipedia.org/wiki/Arrows_(Unicode_block)
static char *rightArrowUnicodeStr = "\u2192";
static char *downArrowUnicodeStr = "\u2193";
static char *leftArrowUnicodeStr = "\u2190";
static char *upArrowUnicodeStr = "\u2191";
// https://www.fileformat.info/info/unicode/char/1f5fc/index.htm
static char *towerUnicodeStr = "\U0001F5FC";
// Html
// see https://en.wikipedia.org/wiki/Arrows_(Unicode_block)
static char *rightArrowHtml = "→";
static char *downArrowHtml = "↓";
static char *leftArrowHtml = "←";
static char *upArrowHtml = "↑";
// https://www.fileformat.info/info/unicode/char/1f5fc/index.htm
static char *towerHtml = "🗼";
// The information about a position in the maze
struct PositionInfo {
// blocked: 1
// not blocked: 0,
int blocked;
/////////// used when exploring the maze ///////
// which row
int r;
// which col
int c;
// the state of the current position
PositionState state;
// points to a unicode string, e.g., upArrowUnicodeStr
char *dirStr;
char *dirStrHtml;
};
static int map[ROWS][COLS] = {
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 1, 1, 1, 0},
{1, 1, 1, 1, 0, 0, 1, 0, 1, 0},
};
static struct PositionInfo maze[ROWS][COLS];
static struct PositionInfo *pStartPos = &maze[1][0];
// For PrintMaze()
static int cnt = 0;
static void InitMaze(void) {
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
maze[r][c].blocked = map[r][c];
maze[r][c].r = r;
maze[r][c].c = c;
maze[r][c].state = NOT_VISITED;
maze[r][c].dirStr = NULL;
maze[r][c].dirStrHtml = NULL;
}
}
cnt = 0;
}
static int IsLegalPosition(int r, int c) {
return (r >= 0) && (r < ROWS) && (c >= 0) && (c < COLS);
}
static int IsExitPosition(int r, int c) {
if (IsLegalPosition(r, c)) {
return (r == ROWS - 2) && (c == COLS - 1);
} else {
return 0;
}
}
static long imgCnt = 0;
static void PrintMaze(char *stepName) {
printf("------------------------- %s %d ----------------------------\n", stepName, cnt);
cnt++;
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
if (maze[r][c].blocked) {
printf("X ");
}
else if (IsExitPosition(r, c)) {
printf("%s ", towerUnicodeStr);
}
else {
if (maze[r][c].dirStr != NULL) {
printf("%s ", maze[r][c].dirStr);
} else {
printf(" ");
}
}
}
if (r == ROWS - 1)
printf("\n");
else
printf("\n\n");
}
printf("---------------------------------------------------------------\n\n\n");
// generate an image
MazeGenOneImage("maze", "images/HtmlMaze", imgCnt);
imgCnt++;
}
/*
If (r, c) is a legal position, it is not blocked and it has not been explored yet,
then
push its position information onto the data stack
*/
static void PushAdjacentPosition(struct Stack *pStack, int r, int c, PositionState initState) {
if (IsLegalPosition(r, c)) {
if (!maze[r][c].blocked && (maze[r][c].state == NOT_VISITED)) {
struct PositionInfo *pPos = &maze[r][c];
pPos->state = initState;
StackPush(pStack, pPos);
}
}
}
/*
State transition:
TO_RIGHT -> TO_DOWN -> TO_LEFT -> TO_UP -> FINISHED
*/
void ExploreMaze(void) {
struct Stack *pStack = CreateStack();
// Initialize the maze
InitMaze();
char *stepName = "Step";
PrintMaze(stepName);
// Push the start position
pStartPos->state = TO_RIGHT;
StackPush(pStack, pStartPos);
while (!StackIsEmpty(pStack)) {
struct PositionInfo *pCurPos = StackPeek(pStack);
if (IsExitPosition(pCurPos->r, pCurPos->c)) {
break;
}
switch(pCurPos->state) {
case TO_RIGHT:
pCurPos->dirStr = rightArrowUnicodeStr;
pCurPos->dirStrHtml = rightArrowHtml;
PushAdjacentPosition(pStack, pCurPos->r, pCurPos->c + 1, TO_RIGHT);
// When this position becomes the top element on the stack,
// we need to go downward.
pCurPos->state = TO_DOWN;
break;
case TO_DOWN:
pCurPos->dirStr = downArrowUnicodeStr;
pCurPos->dirStrHtml = downArrowHtml;
PushAdjacentPosition(pStack, pCurPos->r + 1, pCurPos->c, TO_RIGHT);
// When this position becomes the top element on the stack,
// we need to go leftward.
pCurPos->state = TO_LEFT;
break;
case TO_LEFT:
pCurPos->dirStr = leftArrowUnicodeStr;
pCurPos->dirStrHtml = leftArrowHtml;
PushAdjacentPosition(pStack, pCurPos->r, pCurPos->c - 1, TO_RIGHT);
// When this position becomes the top element on the stack,
// we need to go upward.
pCurPos->state = TO_UP;
break;
case TO_UP:
pCurPos->dirStr = upArrowUnicodeStr;
pCurPos->dirStrHtml = upArrowHtml;
PushAdjacentPosition(pStack, pCurPos->r - 1, pCurPos->c, TO_RIGHT);
// When this position becomes the top element on the stack,
// we have finished all directions.
pCurPos->state = FINISHED;
break;
case FINISHED:
pCurPos->dirStr = NULL;
pCurPos->dirStrHtml = NULL;
StackPop(pStack);
break;
default:
break;
}
PrintMaze(stepName);
}
ReleaseStack(pStack);
PrintMaze(stepName);
}
#if 0
/*
This function returns the next unexplored state randomly and also updates pCurPos->state.
*/
static PositionState NextUnexploredState(struct PositionInfo *pCurPos) {
long x;
long state = (long) (pCurPos->state);
assert(pCurPos->state != FINISHED);
// randomly generate an integer x in {0, 1, 2, 3},
// such that (1 << x) represents a state which has not been explored yet.
do {
x = random();
x %= 4;
} while (((1 << x) & state) != 0);
PositionState nextState = (PositionState)(1 << x);
pCurPos->state |= nextState;
return nextState;
}
void ExploreMazeRandomly(void) {
struct Stack *pStack = CreateStack();
// Initialize the maze
InitMaze();
char *stepName = "Random Step";
PrintMaze(stepName);
// pStartPos->state = NOT_VISITED;
StackPush(pStack, pStartPos);
while (!StackIsEmpty(pStack)) {
struct PositionInfo *pCurPos = StackPeek(pStack);
if (IsExitPosition(pCurPos->r, pCurPos->c)) {
break;
}
PositionState nextState;
if (pCurPos->state != FINISHED) {
nextState = ______Q1______;
} else {
nextState = FINISHED;
}
//
switch(nextState) {
case TO_RIGHT:
pCurPos->dirStr = rightArrowUnicodeStr;
pCurPos->dirStrHtml = rightArrowHtml;
______Q2______;
break;
case TO_DOWN:
pCurPos->dirStr = downArrowUnicodeStr;
pCurPos->dirStrHtml = downArrowHtml;
______Q3______;
break;
case TO_LEFT:
pCurPos->dirStr = leftArrowUnicodeStr;
pCurPos->dirStrHtml = leftArrowHtml;
______Q4______;
break;
case TO_UP:
pCurPos->dirStr = upArrowUnicodeStr;
pCurPos->dirStrHtml = upArrowHtml;
______Q5______;
break;
case FINISHED:
pCurPos->dirStr = NULL;
pCurPos->dirStrHtml = NULL;
StackPop(pStack);
break;
default:
break;
}
PrintMaze(stepName);
}
ReleaseStack(pStack);
PrintMaze(stepName);
}
#endif
////////////////////////////// Maze2Dot (for visualizing the algorithm) ///////////////////////////////////////
static void PrintHtmlTable(FILE *dotFile) {
fprintf(dotFile, "<table border=\"0\" cellborder=\"1\" cellspacing=\"0\" cellpadding=\"0\"> \n");
for (int r = 0; r < ROWS; r++) {
fprintf(dotFile, "<tr> \n");
for (int c = 0; c < COLS; c++) {
if (maze[r][c].blocked) {
// Green block
fprintf(dotFile, "<td width=\"30\" height=\"30\" bgcolor=\"green\"></td> \n");
}
else if (IsExitPosition(r, c)) {
fprintf(dotFile, "<td width=\"30\" height=\"30\">%s</td> \n", towerHtml);
}
else {
struct PositionInfo *pCurPos = &(maze[r][c]);
if (pCurPos->dirStrHtml) {
fprintf(dotFile, "<td width=\"30\" height=\"30\">%s</td> \n", pCurPos->dirStrHtml);
} else {
fprintf(dotFile, "<td width=\"30\" height=\"30\" bgcolor=\"white\"></td> \n");
}
}
}
fprintf(dotFile, "</tr> \n");
}
fprintf(dotFile, "</table> \n");
}
/*
Dot Files
*/
void Maze2Dot( char *filePath,
char *graphName,
int displayVisited) {
(void) displayVisited;
FILE *dotFile = fopen(filePath, "w");
if (dotFile) {
fprintf(dotFile, "digraph %s { \n", graphName);
fprintf(dotFile, "Maze [shape=none, margin=0, label=< \n");
PrintHtmlTable(dotFile);
fprintf(dotFile, " >]; \n");
fprintf(dotFile, "} \n");
fclose(dotFile);
}
}
#define FILE_NAME_LEN 255
void MazeGenOneImage(char *graphName, char *fileName, long seqNo) {
char dotFileName[FILE_NAME_LEN+1] = {0};
char pngFileName[FILE_NAME_LEN+1] = {0};
char command[(FILE_NAME_LEN+1)*4] = {0};
snprintf(dotFileName, FILE_NAME_LEN, "%s_%04ld.dot", fileName, seqNo);
snprintf(pngFileName, FILE_NAME_LEN, "%s_%04ld.png", fileName, seqNo);
Maze2Dot(dotFileName, graphName, 1);
snprintf(command, FILE_NAME_LEN*4, "dot -T png %s -o %s", dotFileName, pngFileName);
//printf("%s\n", command);
// Execute the command in a child process (fork() + exec() on Linux)
system(command);
}