forked from Parrva-Shah/Maze_Pathfinding_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar_Solver.cpp
More file actions
122 lines (94 loc) · 3.46 KB
/
Copy pathAStar_Solver.cpp
File metadata and controls
122 lines (94 loc) · 3.46 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
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
#include "AStar_Solver.h"
#include <limits>
#include <iostream>
using namespace std;
AStar_Solver::AStar_Solver(const Maze& maze)
: Solver(maze, 'A')
{
int rows = maze.getRows();
int cols = maze.getCols();
// Initialize scores and visited grid
//Using a 2D dynamic array (std::vector)
gScore.assign(rows, vector<vector<int>::value_type>(cols, numeric_limits<int>::max()));
//Using a 2D boolean matrix
visited.assign(rows, vector<bool>(cols,false));
gScore[start.first][start.second] = 0;
// Push starting node with its f-score
int fStart = heuristic(start.first, start.second);
openSet.push({fStart, start});//a priority_queue (binary heap) that always exposes the node with the lowest
// Start the algorithm's timer
m_clock.restart();
}
int AStar_Solver::heuristic(int r, int c) const {
// Manhattan distance
return abs(goal.first - r) + abs(goal.second - c);
}
void AStar_Solver::step() {
// If path found earlier, now backtracking the parent pointers
if (currentState == State::TRACING_PATH) {
// Count this node as part of the final path
m_pathLength++;
if (tracePos == start) {
currentState = State::DONE;
return;
}
if (grid[tracePos.first][tracePos.second] != 'E') {
grid[tracePos.first][tracePos.second] = 'X'; // Mark final solution path
}
tracePos = parent[tracePos.first][tracePos.second];
return;
}
if (currentState != State::SEARCHING) return;
// No more nodes to explore
if (openSet.empty()) {
currentState = State::DONE;
found = false;
// Stop the clock if the search fails
m_timeTaken = m_clock.getElapsedTime();
return;
}
while (!openSet.empty()) {
auto current = openSet.top();
openSet.pop();
auto [r, c] = current.pos;
if (visited[r][c]) continue;
visited[r][c] = true;
// Increase node exploration count
m_nodesExplored++;
// Mark this cell as explored
if (grid[r][c] == ' ')
grid[r][c] = symbol;
// Goal reached → switch to tracing mode
if (r == goal.first && c == goal.second) {
found = true;
currentState = State::TRACING_PATH;
tracePos = goal;
// Stop the clock on success
m_timeTaken = m_clock.getElapsedTime();
return;
}
// Explore all 4 neighbours
for (auto [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!isInside(grid, nr, nc)) continue;
if (grid[nr][nc] == '#') continue;
int tentativeG = gScore[r][c] + 1;
// Found a better path to neighbour
if (tentativeG < gScore[nr][nc]) {
gScore[nr][nc] = tentativeG;
parent[nr][nc] = {r, c};
int f = tentativeG + heuristic(nr, nc);
openSet.push({f, {nr, nc}});
}
}
// Process only one node per step() call (good for visualization)
return;
}
// This part is likely unreachable if openSet.empty() is checked above,
// but we stop the clock here just in case.
currentState = State::DONE;
found = false;
if (m_timeTaken == sf::Time::Zero) { // Only set if not already set
m_timeTaken = m_clock.getElapsedTime();
}
}