-
Notifications
You must be signed in to change notification settings - Fork 1
/
maze-solve.c
192 lines (170 loc) · 5.12 KB
/
maze-solve.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG
#include "bool.h"
#include "try.h"
#include "maze.h"
/**
* Solves the maze from a given start room.
*
* \param maze The maze to solve.
* \param room The room to start from.
*
* \return true if the maze has a solution, false if not.
*/
bool solveFrom(Maze maze, Room room);
/**
* Prints all solutions to a maze.
*
* \param maze The maze to solve.
* \param room The room to start from.
*
* \return The number of solutions found.
*/
int solveAll(Maze maze, Room room);
/**
* Finds the shortest solution to the maze. The starting depth should be 0 when
* calling the function. If the depth passed/returned is 0, it can be assumed
* that there was no solution to the maze found.
*
* \param maze The maze to solve.
* \param room The room to start from.
* \param depth The starting depth.
* \param shortMaze An initialized maze that will hold a snapshot of the maze
* solved with the shortest path.
*
* \return The depth of the shortest solution (length of the shortest path).
*/
int solveShort(Maze maze, Room room, int depth, Maze shortMaze);
/* argument parsing */
bool parseArguments(int argc, char **argv, bool *shortest, bool *all, char *filename);
void printHelp(void);
int main(int argc, char **argv)
{
bool status, optshort, optall;
char filename[256];
Maze myMaze;
status = EXIT_FAILURE;
optshort = optall = false;
strcpy(filename, "-");
myMaze = Maze_new();
TRY( (parseArguments(argc, argv, &optshort, &optall, filename)) );
TRY(myMaze);
TRY(argc > 1);
TRY(Maze_import(myMaze, filename));
if (optall)
{
int solutions;
solutions = solveAll(myMaze, Maze_getStart(myMaze));
printf("Found %i solutions.\n", solutions);
}
else if (optshort)
{
Maze shortMaze;
int depth;
shortMaze = Maze_new();
if ( (depth = solveShort(myMaze, Maze_getStart(myMaze), 0, shortMaze)) )
{
puts("Shortest solution:");
Maze_print(shortMaze);
printf("Path length: %i\n", depth);
}
else puts("No solution found.");
}
else if (solveFrom(myMaze, Maze_getStart(myMaze)))
{
puts("Solution found!");
Maze_replaceMarkers(myMaze, Room_deadend, Room_cleared);
Maze_print(myMaze);
}
else puts("No solution found.");
status = EXIT_SUCCESS;
FINALLY:
Maze_free(&myMaze);
return status;
}
bool solveFrom(Maze maze, Room room)
{
int i, nadjacent;
char marker;
Room adjacent[4];
marker = Maze_getMarker(room);
if (room == Maze_getFinish(maze)) return true;
if (marker == Room_visited || marker == Room_deadend) return false;
Maze_setMarker(room, Room_visited);
Maze_print(maze);
getchar();
nadjacent = Maze_getAdjacent(maze, room, adjacent, false);
for (i = 0; i < nadjacent; i++)
if (solveFrom(maze, adjacent[i])) return true;
Maze_setMarker(room, Room_deadend);
return false;
}
int solveAll(Maze maze, Room room)
{
int i, nadjacent;
static int solutionCount;
char marker;
Room adjacent[4];
marker = Maze_getMarker(room);
if (marker == Room_visited) return solutionCount;
if (room == Maze_getFinish(maze))
{
printf("Solution #%i:\n", ++solutionCount);
Maze_print(maze);
getchar();
return solutionCount;
}
Maze_setMarker(room, Room_visited);
nadjacent = Maze_getAdjacent(maze, room, adjacent, false);
for (i = 0; i < nadjacent; i++)
solveAll(maze, adjacent[i]);
Maze_setMarker(room, Room_cleared);
return solutionCount;
}
int solveShort(Maze maze, Room room, int depth, Maze shortMaze)
{
int i, nadjacent;
static int shortDepth, solutionCount;
char marker;
Room adjacent[4];
marker = Maze_getMarker(room);
if (marker == Room_visited) return shortDepth;
if (room == Maze_getFinish(maze))
{
if (depth < shortDepth || solutionCount == 0)
{
Maze_copy(shortMaze, maze);
shortDepth = depth;
}
solutionCount++;
return shortDepth;
}
Maze_setMarker(room, Room_visited);
nadjacent = Maze_getAdjacent(maze, room, adjacent, false);
for (i = 0; i < nadjacent; i++)
solveShort(maze, adjacent[i], depth + 1, shortMaze);
Maze_setMarker(room, Room_cleared);
return shortDepth;
}
bool parseArguments(int argc, char **argv, bool *optshort, bool *optall, char *filename)
{
int i;
for (i = 1; i < argc; i++)
{
if (strcmp("-help", argv[i]) == 0) {printHelp(); return false;}
if (strcmp("-h", argv[i]) == 0) {printHelp(); return false;}
if (strcmp("-short", argv[i]) == 0) *optshort = true;
if (strcmp("-all", argv[i]) == 0) *optall = true;
else strcpy(filename, argv[i]);
}
return true;
}
void printHelp(void)
{
fprintf(stderr, "USAGE: maze-solve [-short] [-all] <filename>\n");
fprintf(stderr, " where -short shows the shortest solution,\n");
fprintf(stderr, " -all shows all solutions\n");
fprintf(stderr, " no flags solves the maze in filename step-by-step\n");
}