-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.cpp
More file actions
169 lines (140 loc) · 5.68 KB
/
Algorithms.cpp
File metadata and controls
169 lines (140 loc) · 5.68 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
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
#include "Algorithms.h"
bool Algorithms::isValidCell(int x, int y) {
return x >= 0 && x < Constants::COLS && y >= 0 && y < Constants::ROWS;
}
bool Algorithms::depthFirstSearch(int grid[Constants::ROWS][Constants::COLS],
sf::Vector2i startCell,
sf::Vector2i goalCell,
std::vector<sf::Vector2i>& path) {
path.clear();
std::vector<std::vector<bool>> visited(Constants::ROWS, std::vector<bool>(Constants::COLS, false));
std::stack<sf::Vector2i> stack;
std::vector<std::vector<sf::Vector2i>> parent(Constants::ROWS, std::vector<sf::Vector2i>(Constants::COLS, {-1, -1}));
// Directions: up, right, down, left
const std::vector<sf::Vector2i> DIRECTIONS = {
{0, -1}, {1, 0}, {0, 1}, {-1, 0}
};
stack.push(startCell);
visited[startCell.y][startCell.x] = true;
while (!stack.empty()) {
sf::Vector2i current = stack.top();
stack.pop();
if (current.x == goalCell.x && current.y == goalCell.y) {
// Reconstruct path
sf::Vector2i pos = goalCell;
while (pos.x != startCell.x || pos.y != startCell.y) {
path.push_back(pos);
pos = parent[pos.y][pos.x];
}
path.push_back(startCell);
std::reverse(path.begin(), path.end());
return true;
}
// Try all possible directions
for (auto& dir : DIRECTIONS) {
int newX = current.x + dir.x;
int newY = current.y + dir.y;
if (isValidCell(newX, newY) && !visited[newY][newX] && grid[newY][newX] != Constants::WALL) {
stack.push({newX, newY});
visited[newY][newX] = true;
parent[newY][newX] = current;
}
}
}
return false;
}
bool Algorithms::breadthFirstSearch(int grid[Constants::ROWS][Constants::COLS],
sf::Vector2i startCell,
sf::Vector2i goalCell,
std::vector<sf::Vector2i>& path) {
path.clear();
std::vector<std::vector<bool>> visited(Constants::ROWS, std::vector<bool>(Constants::COLS, false));
std::vector<std::vector<sf::Vector2i>> parent(Constants::ROWS, std::vector<sf::Vector2i>(Constants::COLS, {-1, -1}));
std::queue<sf::Vector2i> queue;
// Directions: up, right, down, left
const std::vector<sf::Vector2i> DIRECTIONS = {
{0, -1}, {1, 0}, {0, 1}, {-1, 0}
};
queue.push(startCell);
visited[startCell.y][startCell.x] = true;
while (!queue.empty()) {
sf::Vector2i current = queue.front();
queue.pop();
if (current.x == goalCell.x && current.y == goalCell.y) {
// Reconstruct path
sf::Vector2i pos = goalCell;
while (pos.x != startCell.x || pos.y != startCell.y) {
path.push_back(pos);
pos = parent[pos.y][pos.x];
}
path.push_back(startCell);
std::reverse(path.begin(), path.end());
return true;
}
for (auto& dir : DIRECTIONS) {
int newX = current.x + dir.x;
int newY = current.y + dir.y;
if (isValidCell(newX, newY) && !visited[newY][newX] && grid[newY][newX] != Constants::WALL) {
queue.push({newX, newY});
visited[newY][newX] = true;
parent[newY][newX] = current;
}
}
}
return false;
}
bool Algorithms::dijkstraAlgorithm(int grid[Constants::ROWS][Constants::COLS],
sf::Vector2i startCell,
sf::Vector2i goalCell,
std::vector<sf::Vector2i>& path) {
path.clear();
// Node struct for priority queue
struct Node {
int x, y, cost;
bool operator>(const Node& other) const {
return cost > other.cost;
}
};
// Directions: up, right, down, left
const std::vector<sf::Vector2i> DIRECTIONS = {
{0, -1}, {1, 0}, {0, 1}, {-1, 0}
};
std::priority_queue<Node, std::vector<Node>, std::greater<Node>> pq;
std::vector<std::vector<int>> distance(Constants::ROWS, std::vector<int>(Constants::COLS, INT_MAX));
std::vector<std::vector<sf::Vector2i>> parent(Constants::ROWS, std::vector<sf::Vector2i>(Constants::COLS, {-1, -1}));
pq.push({startCell.x, startCell.y, 0});
distance[startCell.y][startCell.x] = 0;
while (!pq.empty()) {
Node current = pq.top();
pq.pop();
// If we've reached the goal
if (current.x == goalCell.x && current.y == goalCell.y) {
// Reconstruct path
sf::Vector2i pos = goalCell;
while (pos.x != startCell.x || pos.y != startCell.y) {
path.push_back(pos);
pos = parent[pos.y][pos.x];
}
path.push_back(startCell);
std::reverse(path.begin(), path.end());
return true;
}
// If we've already found a better path
if (current.cost > distance[current.y][current.x]) {
continue;
}
for (auto& dir : DIRECTIONS) {
int newX = current.x + dir.x;
int newY = current.y + dir.y;
if (isValidCell(newX, newY) && grid[newY][newX] != Constants::WALL) {
int newCost = current.cost + 1;
if (newCost < distance[newY][newX]) {
distance[newY][newX] = newCost;
parent[newY][newX] = {current.x, current.y};
pq.push({newX, newY, newCost});
}
}
}
}
return false;
}