-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGreedyBestFirst_Solver.cpp
More file actions
112 lines (88 loc) · 3.14 KB
/
Copy pathGreedyBestFirst_Solver.cpp
File metadata and controls
112 lines (88 loc) · 3.14 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
#include "GreedyBestFirst_Solver.h"
#include <limits>
#include <iostream>
GreedyBestFirst_Solver::GreedyBestFirst_Solver(const Maze& maze)
: Solver(maze, 'G') // Use 'G' as the symbol
{
int rows = maze.getRows();
int cols = maze.getCols();
visited.assign(rows, std::vector<bool>(cols, false));
// parent is in base class
int hStart = heuristic(start.first, start.second);
openSet.push({hStart, start});// openSet is a std::priority_queue<Node> (min-heap by heuristic).
// Start the algorithm's timer
m_clock.restart();
}
int GreedyBestFirst_Solver::heuristic(int r, int c) const {
// Manhattan distance heuristic
return std::abs(goal.first - r) + std::abs(goal.second - c);
}
// heuristic(r,c) computes the Manhattan distance from (r,c) to the goal.
void GreedyBestFirst_Solver::step() {
// Path Tracing
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'; // 'X' for final path
}
tracePos = parent[tracePos.first][tracePos.second];
return;
}
if (currentState != State::SEARCHING) return;
// Searching
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; // Already processed
// Increase no. of nodes explored
m_nodesExplored++;
visited[r][c] = true;
// Color when processing
if (grid[r][c] == ' ')
grid[r][c] = symbol;
// Found the goal
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; // Exit step
}
// Explore neighbors
for (auto [dr, dc] : directions) {
int nr = r + dr, nc = c + dc;
if (!isInside(grid, nr, nc)) continue;
char cell = grid[nr][nc];
if (cell == '#') continue;
if (!visited[nr][nc]) { // Only check if visited
parent[nr][nc] = {r, c};
int h = heuristic(nr, nc);
openSet.push({h, {nr, nc}});
// Don't color here, color when popped
}
}
// Return after processing one valid node
return;
}
// If loop finishes, openSet was emptied
currentState = State::DONE;
found = false;
// Make sure clock is stopped even if it fails
if (m_timeTaken == sf::Time::Zero) {
m_timeTaken = m_clock.getElapsedTime();
}
}