-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.cpp
More file actions
55 lines (52 loc) · 1.82 KB
/
MazeSolver.cpp
File metadata and controls
55 lines (52 loc) · 1.82 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
//Thanks to @AnonDrew for making a class out of my recursive solution
#include <ostream>
#include <stack>
#include "MazeSolver.h"
//FRIEND
std::ostream& operator<<(std::ostream& outs, MazeSolver& solution) {
if (!solution.path.empty()) {
DS_Maze::MazeCell* top = solution.path.top();
solution.path.pop();
operator<<(outs, solution);
outs << "->" << top->getCoordinates();
solution.path.push(top);
}
return outs;
}
//PRIVATE MUTATOR
void MazeSolver::solve(DS_Maze::MazeCell* cell) {
testPath.push(cell);
cell->setExplored();
if (cell->isFood()) {
++testFood;
}
//base case
if (cell->isFinish()) {
//case: on first run, path will always be empty; otherwise only change solution if a better one was found
if (path.empty() || food < testFood || (food == testFood && path.size() > testPath.size())) {
path = testPath;
food = testFood;
}
}
//traverses the maze in one direction until no longer possible, then backtracks and tries the most recent alternative
if (cell->hasNorth() && !cell->getNorth()->isExplored()) {
MazeSolver::solve(cell->getNorth());
}
if (cell->hasEast() && !cell->getEast()->isExplored()) {
MazeSolver::solve(cell->getEast());
}
if (cell->hasSouth() && !cell->getSouth()->isExplored()) {
MazeSolver::solve(cell->getSouth());
}
if (cell->hasWest() && !cell->getWest()->isExplored()) {
MazeSolver::solve(cell->getWest());
}
//pops cells up to the most recent multi-directional cell in testPath
testPath.pop();
//allows for exploring previously explored paths but from an alternative approach
cell->setUnExplored();
//make sure testFood is accurate after backtracking
if (cell->isFood()) {
--testFood;
}
}