-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.c
383 lines (330 loc) · 10.5 KB
/
maze.c
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
/** maze.c: structures and functions for mazes.
*
* @author N. Danner
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include "stdlib.h"
#include <time.h>
#include "list356.h"
#include "debug.h"
#include "maze.h"
// Directions.
#define EMPTY 0x0
unsigned char directions[] = {NORTH, EAST, SOUTH, WEST} ;
// Offset into the maze cells based on row and column position.
#define CELL(m, r, c) (*(m->cells + (r*(m->ncols)) + c))
// Offset into the maze cells based on a cell_t.
#define MAZECELL(m, cc) (*(m->cells + (cc->r)*(m->ncols) + (cc->c)))
/** Type of an edge between two cells, for use in Prim's algorithm.
*/
typedef struct _edge_t {
cell_t* a ;
cell_t* b ;
} edge_t ;
/** Type of a maze.
*/
struct _maze_t {
/** Each cell is a bitmask of directions; for a cell c, if c&d != 0
* for a direction d, then there is a passage (no wall) in the direction d.
*/
unsigned char *cells ;
/** Number of rows and columns for this maze.
*/
int nrows, ncols ;
/** The start and end cells of the maze.
*/
cell_t *start, *end ;
} ;
/** Build a maze using Prim's algorithm. After calling, walls will have
* been removed from <code>maze</code> to ensure that there is exactly
* one path between any pair of cells.
*
* @param maze the maze. All walls must be present.
*/
static void build_prim(maze_t* maze) ;
// CELL AND EDGE FUNCTIONS.
/** The cell objects. We do our own "memory management" for cells by
* maintaining a 2D array of cell_t objects; use get_cell to get the
* appropriate object given a row and column.
*/
cell_t* cells = NULL ;;
/** Get a cell given row and column; see maze.h.
*/
cell_t* get_cell(maze_t* m, int r, int c) {
return &cells[r*m->ncols+c] ;
}
/** Test two cells for equality; see maze.h.
*/
int cell_cmp(void* c, void* d) {
return c == d ? 0 : 1 ;
}
/** Test two edges for equality.
*
* @param x1 one edge
* @param x2 another edge
*
* @return 0 if the two edges are equal (i.e., have the same cells
* as endpoints in either order), non-0 otherwise.
*/
static int edge_cmp(void* x1, void* x2) {
edge_t* e1 = x1 ;
edge_t* e2 = x2 ;
return (cell_cmp(e1->a, e2->a) + cell_cmp(e1->b, e2->b))*
(cell_cmp(e1->a, e2->b) + cell_cmp(e1->b, e2->a)) ;
}
/** Get a random integer within a given range.
*
* @param lower the lower limit of the range.
* @param upper the upper limit of the range.
*
* @return a random number in [lower, upper).
*/
static int random_limit(int lower, int upper) {
int val = lower+random()%(upper-lower) ;
return val ;
}
/** Make a maze. See maze.h.
*/
maze_t* make_maze(int nrows, int ncols, long seed) {
// Allocate the array of cell objects.
if (cells != NULL) free(cells) ;
cells = malloc(nrows*ncols*sizeof(cell_t)) ;
for (int r=0; r<nrows; ++r) {
for (int c=0; c<ncols; ++c) {
cells[r*ncols+c].r = r ;
cells[r*ncols+c].c = c ;
}
}
srandom(seed) ;
// Choose start and end cells at random, ensuring that they are not the
// same cell.
maze_t* m = malloc(sizeof(maze_t)) ;
m->cells = malloc(nrows*ncols*sizeof(unsigned char)) ;
m->nrows = nrows ;
m->ncols = ncols ;
m->start = get_cell(m, random_limit(0, nrows), random_limit(0, ncols)) ;
cell_t* end_cell ;
do {
end_cell = get_cell(m, random_limit(0, nrows), random_limit(0, ncols)) ;
} while (end_cell == m->start) ;
m->end = end_cell ;
// Add all possible walls to the maze.
for (int r=0; r<nrows; ++r) {
for (int c=0; c<ncols; ++c) {
CELL(m, r, c) = EMPTY ;
}
}
// Generate the maze.
build_prim(m) ;
return m ;
}
/** Get the start cell of a maze; see maze.h.
*/
cell_t* get_start(maze_t* m) {
return m->start ;
}
/** Get the end cell of a maze; see maze.h.
*/
cell_t* get_end(maze_t* m) {
return m->end ;
}
/** Get the number of rows of a maze; see maze.h.
*/
int get_nrows(maze_t* m) {
return m->nrows ;
}
/** Get the number of columns of a maze; see maze.h.
*/
int get_ncols(maze_t* m) {
return m->ncols ;
}
/** Check for a path to an adjacent cell; see maze.h.
*/
bool has_path(maze_t* m, cell_t* c, unsigned char d) {
return (MAZECELL(m, c) & d) != 0 ;
}
/** Check for a path to an adjacent cell; see maze.h.
*/
bool has_wall(maze_t* m, cell_t* c, unsigned char d) {
return !has_path(m, c, d) ;
}
/** Get the cell in a given direction from a given cell.
*
* @param cell the given cell.
* @param direction the direction
*
* @return the cell that is adjacent to <code>cell</code> in direction
* <code>direction</code>.
*/
static cell_t* get_neighbor(maze_t* m, cell_t* cell, unsigned char direction) {
switch (direction) {
case NORTH:
return get_cell(m, 1+cell->r, cell->c) ;
case EAST:
return get_cell(m, cell->r, 1+cell->c) ;
case SOUTH:
return get_cell(m, cell->r-1, cell->c) ;
case WEST:
return get_cell(m, cell->r, cell->c-1) ;
default:
assert(0) ;
}
assert(0) ;
// Kill "control reaches end" compiler warnings with -DNDEBUG.
return NULL ;
}
/** Get the direction from one cell to another.
*
* @param a a cell.
* @param b a cell that is adjacent to a.
*
* @return the direction from a to b.
*/
static unsigned char get_direction(cell_t* a, cell_t* b) {
if (b->r == a->r+1) return NORTH ;
if (b->r == a->r-1) return SOUTH ;
if (b->c == a->c+1) return EAST ;
if (b->c == a->c-1) return WEST ;
assert(0) ;
// Kill "control reaches end" compiler warnings with -DNDEBUG.
return 0 ;
}
/** Get the opposite direction from a given direction.
*
* @param direction any direction.
*
* @return the opposite direction from <code>direction</code>.
*/
static unsigned char opposite(unsigned char direction) {
switch (direction) {
case NORTH:
return SOUTH ;
case EAST:
return WEST ;
case SOUTH:
return NORTH;
case WEST:
return EAST ;
}
assert(0) ;
// Kill "control reaches end" compiler warnings with -DNDEBUG.
return 0 ;
}
/** Check whether there is a cell in the given direction from a given
* cell.
*
* @param m a maze.
* @param c a cell in <code>m</code>.
* @param d a direction.
*
* @return <code>true</code> if there is a cell in direction <code>d</code>
* from <code>c</code>, <code>false</code> otherwise.
*/
static bool is_cell(maze_t* m, cell_t* c, unsigned char d) {
switch(d) {
case NORTH:
return (c->r < m->nrows-1) ;
case EAST:
return (c->c < m->ncols-1) ;
case SOUTH:
return (c->r >= 1) ;
case WEST:
return (c->c >= 1) ;
}
assert(0) ;
// Kill "control reaches end" compiler warnings with -DNDEBUG.
return false ;
}
/** Remove a wall from the maze.
*
* @param m a maze.
* @param c a cell in <code>m</code>.
* @param d a direction.
*/
static void remove_wall(maze_t* m, cell_t* c, unsigned char d) {
MAZECELL(m, c) |= d ;
cell_t* adj = get_neighbor(m, c, d) ;
MAZECELL(m, adj) |= opposite(d) ;
}
/** Build the maze by removing walls according to Prim's algorithm.
*
* @param maze a maze with all walls present.
*/
static void build_prim(maze_t* maze) {
// MST edges and cells. If (a, b) in mst_edges, then (b, a) not in
// mst_edges. (a, b) in mst_edges iff a, b both in mst_cells.
list356_t* mst_edges = make_list() ;
list356_t* mst_cells = make_list() ;
// The frontier. This is the collection of edges between cells in the MST
// and cells not in the MST. If (a, b) in frontier, then a in mst_cells
// and b not in mst_cells.
list356_t* frontier = make_list() ;
// Choose two adjacent cells at random to put into the MST, then
// populate the frontier accordinately. For simplicitly, choose a
// cell in the interior of the maze, then randomly choose a direction
// for the other cell.
cell_t* start =
get_cell(maze, random_limit(1, maze->nrows-1),
random_limit(1, maze->ncols-1));
unsigned char direction = 1 << random_limit(0, 4) ;
cell_t* next = get_neighbor(maze, start, direction) ;
/*
debug("Removing (%d, %d) - (%d, %d).\n",
start->r, start->c, next->r, next->c) ;
*/
remove_wall(maze, start, direction) ;
edge_t init_edge = (edge_t){start, next} ;
lst_add(mst_edges, &init_edge) ;
lst_add(mst_cells, start) ;
lst_add(mst_cells, next) ;
for (int d=0; d<4; ++d) {
if (directions[d] != direction && is_cell(maze, start, directions[d])) {
cell_t* c = get_neighbor(maze, start, directions[d]) ;
edge_t* e = malloc(sizeof(edge_t)) ;
e->a = start ; e->b = c ;
lst_add(frontier, e) ;
}
}
for (int d=0; d<4; ++d) {
if (directions[d] != opposite(direction)
&& is_cell(maze, next, directions[d])) {
cell_t* c = get_neighbor(maze, next, directions[d]) ;
edge_t* e = malloc(sizeof(edge_t)) ;
e->a = next ; e->b = c ;
lst_add(frontier, e) ;
}
}
// As long as we don't have all the cells in the MST, choose an
// edge in the frontier at random. Put the edge in the MST
// and compute the new edges to add to the frontier.
while (lst_size(mst_cells) < (maze->nrows)*(maze->ncols)) {
int p = random_limit(0, lst_size(frontier)) ;
edge_t* edge = lst_get(frontier, p) ;
cell_t* old_cell = edge->a ;
cell_t* new_cell = edge->b ;
/*
debug("Removing (%d, %d) - (%d, %d).\n",
old_cell->r, old_cell->c, new_cell->r, new_cell->c) ;
*/
remove_wall(maze, old_cell, get_direction(old_cell, new_cell)) ;
lst_add(mst_edges, edge) ;
lst_add(mst_cells, new_cell) ;
for (int d=0; d<4; ++d) {
unsigned char dir = directions[d] ;
if (is_cell(maze, new_cell, dir)) {
cell_t* adj_cell = get_neighbor(maze, new_cell, dir) ;
edge_t* edge2 = malloc(sizeof(edge_t)) ;
edge2->a = new_cell ; edge2->b = adj_cell ;
if (lst_contains(mst_cells, adj_cell, cell_cmp)) {
lst_remove(frontier, edge2, edge_cmp) ;
if (adj_cell != old_cell) free(edge2) ;
}
else {
lst_add(frontier, edge2) ;
}
}
}
}
}