-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSolver.cpp
More file actions
69 lines (56 loc) · 1.73 KB
/
DFSolver.cpp
File metadata and controls
69 lines (56 loc) · 1.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "DFSolver.h"
#include "Maze.h"
#include <stack>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void DFSolver::solve(const Maze &maze) {
pair<int,int> start = maze.getStart();
pair<int,int> end = maze.getEnd();
vector<vector<int>> dist(maze.getRows(), vector<int>(maze.getCols(), -1));
map<pair<int,int>, pair<int,int>> parent;
stack<pair<int,int>> st;
st.push(start);
dist[start.first][start.second] = 0;
parent[start] = start;
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
while(!st.empty()) {
pair<int,int> top = st.top();
st.pop();
int x = top.first, y = top.second;
// Show current position being explored
cout << "Exploring: (" << x << "," << y << ")\n";
maze.printMaze(top);
if (top == end) {
cout << "Found the end!\n";
break;
}
for (int k = 3; k >= 0; k--) {
int nx = x + dx[k], ny = y + dy[k];
pair<int,int> next = {nx, ny};
if (maze.isValid(nx, ny) && dist[nx][ny] == -1) {
dist[nx][ny] = dist[x][y] + 1;
parent[next] = top;
st.push(next);
}
}
}
// Reconstruct path
if (parent.find(end) == parent.end()) {
cout << "No path found with DFS!\n";
return;
}
vector<pair<int,int>> path;
for (pair<int,int> cur = end; cur != start; cur = parent[cur]) {
path.push_back(cur);
}
path.push_back(start);
reverse(path.begin(), path.end());
// Show final path
cout << "\nFinal path:\n";
maze.printMaze(end, path);
cout << "\nDFS found a path of length " << path.size()-1 << ".\n";
}