-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_solver_helpers.c
609 lines (488 loc) · 13.4 KB
/
maze_solver_helpers.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include <stdlib.h>
#include <stdbool.h>
#include "common.h"
#include "bmp/bmp_helpers.h"
#include "maze_solver_helpers.h"
#include "maze_graph_bridge.h"
#include "a_star/frontier/pqueue.h"
#include "../../my_math/math.h"
#if defined KS_MAZE_SOLVER_DEBUG || defined KS_MAZE_SOLVER_DEBUG_FIND_SHORTEST_PATH
#include <stdio.h>
#endif
#ifdef KS_MAZE_SOLVER_DEBUG
#include <limits.h>
#endif
#ifdef KS_MAZE_SOLVER_DEBUG
/**
* Returns non-zero value if the given pixel in the maze is a hurdle pixel.
* Else returns 0.
*/
inline static
int is_hurdle_pixel(struct maze_image *const maze, unsigned pixel)
{
#ifdef KS_MAZE_SOLVER_DEBUG
if (pixel >= maze->pixels)
{
fprintf(stderr, "is_hurdle_pixel: Invalid pixel %u\n", pixel);
exit(EXIT_FAILURE);
}
#endif
const unsigned char *const pixel_byte = maze->data + pixel;
// It's enough to check one byte for now
return ((*pixel_byte&HURDLE_PIXEL) == (*pixel_byte)) ? 1 : 0;
}
#endif
/**
* Returns non-zero value if the given pixel in the maze is a clear pixel.
* Else returns 0.
*
* Note: An out-of-bound pixel is "not" considered to be a clear pixel.
* (This is done to simplify adjacency initialization logic)
*/
inline static
int is_clear_pixel(struct maze_image *const maze, unsigned pixel)
{
if (pixel >= maze->pixels)
{
return 0;
}
const unsigned char *const pixel_byte = maze->data + pixel;
// It's enough to check one byte for now
return ((*pixel_byte&CLEAR_PIXEL) == CLEAR_PIXEL) ? 1 : 0;
}
/**
* Finds a 'gate' in the given range. A gate is the only clear pixel in the given range.
*
* On success, returns the value of the pixel of the gate cast to a long.
* If one is not found returns -1;
*/
static
long find_gate(struct maze_image *const maze, unsigned start_pixel, unsigned end_pixel)
{
long gate = -1;
#ifdef KS_MAZE_SOLVER_DEBUG_FIND_GATE
bool found_gate = false;
#endif
// find the pixel of the start gate
for (unsigned pixel = start_pixel; pixel<=end_pixel; pixel++)
{
unsigned char *const pixel_ptr = maze->data + pixel;
if ((*pixel_ptr&CLEAR_PIXEL) == CLEAR_PIXEL)
{
#ifdef KS_MAZE_SOLVER_DEBUG_FIND_GATE
if (found_gate)
{
fprintf(stderr, "find_gate: Two start gates found!\n");
exit(EXIT_FAILURE);
}
#endif
gate = pixel;
#ifdef KS_MAZE_SOLVER_DEBUG_FIND_GATE
found_gate = true;
#else
break;
#endif
}
}
return gate;
}
struct openings *find_openings(struct maze_image *const maze)
{
struct openings *const gates = malloc(sizeof(struct openings));
if (gates == NULL)
{
return NULL;
}
long start_gate_pixel = find_gate(maze, 0, maze->width-1),
end_gate_pixel = find_gate(maze, (maze->width)*(maze->height-1), maze->pixels-1);
if (start_gate_pixel == -1 || end_gate_pixel == -1)
{
return NULL;
}
gates->start_gate_pixel = start_gate_pixel;
gates->end_gate_pixel = end_gate_pixel;
return gates;
}
#ifdef KS_MAZE_SOLVER_DEBUG
void print_ascii_maze(struct maze_image *const maze)
{
for (unsigned pixel=0; pixel<maze->pixels; pixel++)
{
if (pixel%maze->width == 0)
{
printf("\n");
}
if (is_clear_pixel(maze, pixel))
{
printf(" ");
}
else if (is_hurdle_pixel(maze, pixel))
{
printf("\u2588"); // unicode block character
}
else
{
fprintf(stderr, "Unexpected Hex!!\n");
exit(EXIT_FAILURE);
}
}
printf("\n");
}
#endif
/**
* Manhattan distance heuristic for A-star.
*/
static inline
unsigned m_dist(const int x1, const int y1,
const int x2, const int y2)
{
#ifdef KS_MAZE_SOLVER_DEBUG_MANHATTAN_DISTANCE
printf("m_dist: x1: %d, y1: %d; x2: %d, y2: %d\n", x1, y1, x2, y2);
#endif
return math_abs(x2-x1)+math_abs(y2-y1);
}
int create_graph(struct maze_image *const maze, struct openings *gates)
{
if (maze == NULL)
{
return 1;
}
const unsigned goal_row = gates->end_gate_pixel/maze->width,
goal_col = gates->end_gate_pixel%maze->width;
#ifdef KS_MAZE_SOLVER_DEBUG
printf("create_graph: sizeof(struct node): %zu\n", sizeof(struct node));
printf("create_graph: sizeof(struct adj_list): %zu\n", sizeof(struct adj_list));
printf("create_graph: sizeof(struct node_list): %zu\n", sizeof(struct node_list));
unsigned clear_pixels=0;
#endif
for (unsigned curr_row = 0; curr_row < maze->height; curr_row++)
{
for (unsigned curr_col = 0; curr_col < maze->width; curr_col++)
{
const unsigned curr_pixel = (curr_row * maze->width) + curr_col;
if (is_clear_pixel(maze, curr_pixel))
{
/* Create the node */
struct node *const n = calloc(1, sizeof(struct node));
if (n == NULL)
{
#ifdef KS_MAZE_SOLVER_DEBUG_CREATE_GRAPH
fprintf(stderr, "create_graph: 'create_node' failed for pixel: %u\n", curr_pixel);
#endif
return 1;
}
n->pixel = curr_pixel;
n->adjlist.adjs = NULL; // possibly not necessary as calloc is used, just in case
n->pi = NULL;
// insert the node into the 'np_list'
if (insert_node(n))
{
#ifdef KS_MAZE_SOLVER_DEBUG_CREATE_GRAPH
fprintf(stderr, "create_graph: 'insert_node' failed for pixel: %u\n", curr_pixel);
#endif
return 1;
}
#ifdef KS_MAZE_SOLVER_DEBUG_CREATE_GRAPH
printf("create_graph: node value: %u\n", n->pixel);
#endif
/* Initialise adjacencies */
// This is not an issue for the start pixel which is in the first row as the
// is_clear_pixel() funtion would return 0 for out-of-bound values.
const unsigned left = curr_pixel - 1,
top = curr_pixel - maze->width;
if (is_clear_pixel(maze, left))
{
#ifdef KS_MAZE_SOLVER_DEBUG_INITIALIZE_ADJACENCIES
printf("initialize_adjacencies: Found adjacencies %u and %u\n",
curr_pixel, left);
#endif
// optimize this using a node_list "cache"
struct node *const left_node = get_node(left);
#ifdef KS_MAZE_SOLVER_DEBUG
if (left_node == NULL)
{
fprintf(stderr, "initialize_adjacencies: Invalid left node: %u for current clear pixel: %u\n",
left, curr_pixel);
exit(EXIT_FAILURE);
}
#endif
if (add_adjacency(n, left_node))
{
return 1;
}
}
if (is_clear_pixel(maze, top))
{
#ifdef KS_MAZE_SOLVER_DEBUG_INITIALIZE_ADJACENCIES
printf("initialize_adjacencies: Found adjacencies %u and %u\n",
curr_pixel, top);
#endif
struct node *const top_node = get_node(top);
#ifdef KS_MAZE_SOLVER_DEBUG
if (top_node == NULL)
{
fprintf(stderr, "initialize_adjacencies: Invalid top node: %u for current clear pixel: %u\n",
top, curr_pixel);
exit(EXIT_FAILURE);
}
#endif
if (add_adjacency(n, top_node))
{
return 1;
}
}
/*
* Initialize nodes with manhattan distance to destination as heuristic value
* for A-star algorithm.
*/
#ifdef KS_MAZE_SOLVER_DEBUG
if (curr_row > INT_MAX ||
curr_col > INT_MAX ||
goal_row > INT_MAX ||
goal_col > INT_MAX)
{
fprintf(stderr, "initialize_nodes: integer overflow\n");
fprintf(stderr, "initialize_nodes: co-ordinates don't fit into an 'int'\n");
exit(EXIT_FAILURE);
}
#endif
#ifdef KS_MAZE_SOLVER_DEBUG_MANHATTAN_DISTANCE
printf("initialize_nodes: finding manhattan distance for pixel: %u\n", curr_pixel);
#endif
n->heuristic = m_dist(curr_row, curr_col, goal_row, goal_col);
#ifdef KS_MAZE_SOLVER_DEBUG
clear_pixels++;
#endif
}
}
}
#ifdef KS_MAZE_SOLVER_DEBUG
printf("create_graph: Totally found %u clear pixels\n", clear_pixels);
#endif
return 0;
}
/**
* Construct the shortest path from the values of the predecessor of each node
* starting from the end node.
*
* Returns the distance of the end node from the start node on success and 0 in case
* of an error.
*/
static
int construct_shortest_path(struct openings *const gates, struct sp_queue_head *const sp)
{
struct node *path_node = get_node(gates->end_gate_pixel);
#ifdef KS_MAZE_SOLVER_DEBUG
if (path_node == NULL)
{
fprintf(stderr, "construct_shortest_path: Invalid pixel node.\n");
exit(EXIT_FAILURE);
}
#endif
const unsigned dest_dist = path_node->src_dist;
#ifdef KS_MAZE_SOLVER_DEBUG
printf("construct_shortest_path: sizeof(struct sp_queue_elem): %zu\n", sizeof(struct sp_queue_elem));
printf("construct_shortest_path: Destination is %u pixels away from the source.\n", dest_dist);
#endif
// sanity check
if (sp == NULL)
{
return 0;
}
while (path_node->pi != NULL)
{
// insert the current path node
struct sp_queue_elem *const path_elem = malloc(sizeof(struct sp_queue_elem));
if (path_elem == NULL)
{
return 0;
}
path_elem->elem = path_node->pixel;
#ifdef KS_MAZE_SOLVER_DEBUG
if (sp_insert_elem(sp, path_elem))
{
fprintf(stderr, "construct_shortest_path: Inserting %u into shortest path queue failed!", path_elem->elem);
exit(EXIT_FAILURE);
}
#else
sp_insert_elem(sp, path_elem);
#endif
path_node = path_node->pi;
}
// insert the source node
struct sp_queue_elem *const source_elem = malloc(sizeof(struct sp_queue_elem));
if (source_elem == NULL)
{
return 0;
}
source_elem->elem = path_node->pixel;
#ifdef KS_MAZE_SOLVER_DEBUG
if (sp_insert_elem(sp, source_elem))
{
fprintf(stderr, "construct_shortest_path: Inserting into shortest path queue failed!");
exit(EXIT_FAILURE);
}
#else
sp_insert_elem(sp, source_elem);
#endif
return dest_dist;
}
unsigned find_shortest_path(struct openings *const gates, struct sp_queue_head *sp)
{
struct node *const start_node = get_node(gates->start_gate_pixel);
#ifdef KS_MAZE_SOLVER_DEBUG
if (start_node == NULL)
{
fprintf(stderr, "construct_shortest_path: Invalid pixel node.\n");
exit(EXIT_FAILURE);
}
unsigned nodes_expanded = 0;
#endif
bool found_dest = false, out_of_mem = false;
// initial setup
start_node->colour = IN_FRONTIER;
// create the frotier queue head
struct min_heap *const frontier = malloc(sizeof(struct min_heap));
#ifdef KS_MAZE_SOLVER_DEBUG
printf("find_shortest_path: sizeof(struct min_heap): %zu\n", sizeof(struct min_heap));
printf("find_shortest_path: sizeof(struct heap_elem): %zu\n", sizeof(struct heap_elem));
#endif
if (frontier == NULL)
{
return 0;
}
initialise_min_heap(frontier);
// insert the start node into the frontier
struct heap_elem *const first = malloc(sizeof(struct heap_elem));
if (first == NULL)
{
out_of_mem = true;
goto CLEANUP;
}
first->val = start_node;
first->key = start_node->heuristic;
#ifdef KS_MAZE_SOLVER_DEBUG
if (min_heap_insert(frontier, first))
{
fprintf(stderr, "find_shortest_path: Inserting into the queue failed!\n");
exit(EXIT_FAILURE);
}
#else
min_heap_insert(frontier, first);
#endif
while (!min_heap_empty(frontier))
{
struct heap_elem *const curr_elem = extract_min(frontier);
#ifdef KS_MAZE_SOLVER_DEBUG
if (curr_elem == NULL)
{
fprintf(stderr, "find_shortest_path: Removing from the queue failed!\n");
exit(EXIT_FAILURE);
}
#endif
// stop expanding and just free obtained memory when,
//
// i) destination has been found (or)
// ii) memory issue occurs
//
if (!(found_dest | out_of_mem)) {
struct node *const curr = curr_elem->val;
for (unsigned adj=0; adj<curr->adjlist.num; adj++)
{
struct node *curr_adj = *(curr->adjlist.adjs + adj);
if (curr_adj->colour == NOT_VISITED)
{
// set the attributes
curr_adj->colour = IN_FRONTIER;
curr_adj->src_dist = curr->src_dist+1;
curr_adj->pi = curr;
// insert the element into the frontier
struct heap_elem *const adj_elem = malloc(sizeof(struct heap_elem));
if (adj_elem == NULL)
{
out_of_mem = true;
break;
}
adj_elem->val = curr_adj;
adj_elem->key = curr_adj->src_dist + curr_adj->heuristic;
#ifdef KS_MAZE_SOLVER_DEBUG_FIND_SHORTEST_PATH
printf("find_shortest_path: heuristic (tie breaker): %u key: %u for pixel: %u\n",
curr_adj->heuristic, adj_elem->key, curr_adj->pixel);
#endif
#ifdef KS_MAZE_SOLVER_DEBUG
if (min_heap_insert(frontier, adj_elem))
{
fprintf(stderr, "find_shortest_path: Inserting into the BFS queue failed!");
exit(EXIT_FAILURE);
}
#else
min_heap_insert(frontier, adj_elem);
#endif
if (curr_adj->pixel == gates->end_gate_pixel)
{
found_dest = true;
break;
}
}
}
curr->colour = VISITIED;
#ifdef KS_MAZE_SOLVER_DEBUG
nodes_expanded++;
#endif
}
free(curr_elem);
}
#ifdef KS_MAZE_SOLVER_DEBUG
printf("find_shortest_path: Totally expanded %u nodes.\n", nodes_expanded);
#endif
CLEANUP:
// free the queue head
free(frontier);
if (out_of_mem)
{
return 0;
}
// construct the shortest path from the values of the predecessors
return construct_shortest_path(gates, sp);
}
void delete_graph(void)
{
if (np_list == NULL)
{
return;
}
// initially free the individual nodes
for (unsigned clear_pixel=0; clear_pixel<np_list_vals; clear_pixel++)
{
struct node *const curr_pixel_node = (*(np_list+clear_pixel))->pixel_node;
free(curr_pixel_node);
}
// now delete the np_list itself
delete_np_list();
}
/**
* Colour the given pixel in the maze.
*/
inline static
void colour_pixel(struct maze_image *const maze, unsigned pixel)
{
unsigned char *const pixel_byte = maze->data + pixel;
static const unsigned char colour_byte = 0x77;
*(pixel_byte) = colour_byte;
}
void colour_path(struct maze_image *const maze, struct sp_queue_head *const sp)
{
while (!sp_queue_empty(sp))
{
struct sp_queue_elem *const curr_elem = sp_remove_elem(sp);
#ifdef KS_MAZE_SOLVER_DEBUG
if (curr_elem == NULL)
{
fprintf(stderr, "colour_pixel: Removal fron the shortest path queue failed!\n");
exit(EXIT_FAILURE);
}
#endif
colour_pixel(maze, curr_elem->elem);
free(curr_elem);
}
}