forked from everfor/Maze_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazeSolver.cpp
186 lines (156 loc) · 5.21 KB
/
MazeSolver.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
#include "MazeGenerator.h"
#include "MazeSolver.h"
#include <cstdio>
#include <ctime>
#include <cmath>
std::vector<int> MazeSolver::SolveMaze(std::vector<std::vector<int> > walls)
{
// The path that solves the maze
std::vector<int> path;
// Implment your algorithm here
// Return the final path
return path;
}
// A simple example algorithm that solves the maze
std::vector<int> MazeSolver::ExampleSolver(std::vector<std::vector<int> > walls)
{
// The path that solves the maze
std::vector<int> path;
// Store the status of visited cells
std::vector<bool> visited (walls.size(), false);
int totalNumber = walls.size(); // Total number of cells
int dimension = (int) sqrt((float)totalNumber); // Get dimension of the maze
int currentCell = 0; // Start from cell 0
path.push_back(currentCell);
while (currentCell < totalNumber - 1) {
visited[currentCell] = true; // Mark current cell as visited
std::vector<int> neighbors;
if (currentCell % dimension != 0 && currentCell > 0) {
// Left neighbor
// If it is adjacent to current cell and has not been visited,
// Add it to valid neighbors list
if (walls[currentCell][0] == 0 && !visited[currentCell - 1]) {
neighbors.push_back(currentCell - 1);
}
}
if (currentCell % dimension != dimension - 1 && currentCell < totalNumber - 1) {
// Right neighbor
// If it is adjacent to current cell and has not been visited,
// Add it to valid neighbors list
if (walls[currentCell][2] == 0 && !visited[currentCell + 1]) {
neighbors.push_back(currentCell + 1);
}
}
if (currentCell >= dimension) {
// Upper neighbor
// If it is adjacent to current cell and has not been visited,
// Add it to valid neighbors list
if (walls[currentCell][1] == 0 && !visited[currentCell - dimension]) {
neighbors.push_back(currentCell - dimension);
}
}
if (currentCell < totalNumber - dimension) {
// Lower neighbor
// If it is adjacent to current cell and has not been visited,
// Add it to valid neighbors list
if (walls[currentCell][3] == 0 && !visited[currentCell + dimension]) {
neighbors.push_back(currentCell + dimension);
}
}
if (neighbors.size() > 0) {
// If there are valid neighbors
// Take the first one and move to it
currentCell = neighbors[0];
path.push_back(currentCell);
} else {
// Otherwise go back to previous cell
path.pop_back();
currentCell = path.back();
}
}
// Return the final path
return path;
}
// Validate the path for a maze
// Returns true if the path is valid, false otherwise
bool MazeSolver::ValidatePath(int dimension, std::vector<std::vector<int> > walls, std::vector<int> path)
{
// Get the path length and total number of cells in a maze
int pathLength = path.size();
int totalCells = walls.size();
// First simple check
// Check the start and end cell
if (path[0] != 0 || path[pathLength - 1] != totalCells - 1) {
return false;
}
// Check along the path to see if it counters any walls
for (int i = 0; i < pathLength - 1; i++) {
// The difference of IDs between next cell and current cell
// Used to determine the relative position of next cell
int difference = path[i + 1] - path[i];
if (difference == 1) {
// The next cell is right to current cell and there is a wall to the right
if (walls[path[i]][2] == 1) {
return false;
}
} else if (difference == -1) {
// The next cell is left to current cell and there is a wall to the left
if (walls[path[i]][0] == 1) {
return false;
}
} else if (difference == dimension) {
// The next cell is lower to current cell
if (walls[path[i]][3] == 1) {
return false;
}
} else if (difference == 0 - dimension) {
// The next cell is upper to current cell
if (walls[path[i]][1] == 1) {
return false;
}
} else {
return false;
}
}
// If the path passes validation then it is good
return true;
}
int main(int argc,char *argv[])
{
// Initialize random seed
time_t seed = time(NULL);
std::cout << "Seed: " << seed << std::endl;
srand(seed);
if(argc != 3) {
std::cout << "Usage: maze <dimension> <tries>" << std::endl;
exit(1);
}
// The dimension of the maze
int dimension = atoi(argv[1]);
int tries = atoi(argv[2]);
if(dimension < 1 || tries < 1) {
std::cout << "Invalid input, please retry" << std::endl;
exit(1);
}
double totalDuration = 0.d;
for(int i = 0 ; i < tries ; i++) {
std::cout << "Starting run #" << (i + 1) << std::endl;
// Generate walls for the maze given the dimension
std::vector<std::vector<int> > walls = MazeGenerator::GenerateMaze(dimension);
std::clock_t startTime;
startTime = std::clock();
// Get the path that solves the maze
std::vector<int> path = MazeSolver::SolveMaze(walls);
double duration = (std::clock() - startTime) / (double) CLOCKS_PER_SEC;
// Path validation
if(!MazeSolver::ValidatePath(dimension, walls, path)) {
std::cout << "Your solution for this run is invalid. Please check your algorithm." << std::endl;
exit(1);
}
totalDuration += duration;
std::cout << "Run #" << (i + 1) << " done." << std::endl;
}
std::cout << "Done! Your average time was " << (totalDuration / tries) << "s, over "
<< tries << " runs on mazes of dimension " << dimension << std::endl;
return 0;
}