Skip to content

Commit 1f14526

Browse files
committed
A* Algorithm
1 parent 7055ce2 commit 1f14526

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Structure to represent a node in the grid
5+
struct Node {
6+
int x, y; // Coordinates
7+
int gCost; // Cost from start node
8+
int hCost; // Heuristic cost to goal
9+
int fCost; // Total cost = g + h
10+
Node* parent; // Pointer to parent node for path reconstruction
11+
12+
Node(int _x, int _y, int _g, int _h, Node* _parent = nullptr)
13+
: x(_x), y(_y), gCost(_g), hCost(_h), parent(_parent) {
14+
fCost = gCost + hCost;
15+
}
16+
};
17+
18+
// Heuristic function: Manhattan distance
19+
int heuristic(int x1, int y1, int x2, int y2) {
20+
return abs(x1 - x2) + abs(y1 - y2);
21+
}
22+
23+
// Check if a position is valid and walkable
24+
bool isValid(int x, int y, int rows, int cols, vector<vector<int>>& grid) {
25+
return x >= 0 && y >= 0 && x < rows && y < cols && grid[x][y] == 0;
26+
}
27+
28+
// A* algorithm to find shortest path
29+
vector<pair<int,int>> aStar(vector<vector<int>>& grid, pair<int,int> start, pair<int,int> goal) {
30+
int rows = grid.size();
31+
int cols = grid[0].size();
32+
33+
auto cmp = [](Node* a, Node* b) { return a->fCost > b->fCost; };
34+
priority_queue<Node*, vector<Node*>, decltype(cmp)> openSet(cmp);
35+
36+
vector<vector<bool>> closedSet(rows, vector<bool>(cols, false));
37+
38+
Node* startNode = new Node(start.first, start.second, 0, heuristic(start.first, start.second, goal.first, goal.second));
39+
openSet.push(startNode);
40+
41+
int dx[] = {-1, 1, 0, 0};
42+
int dy[] = {0, 0, -1, 1};
43+
44+
while (!openSet.empty()) {
45+
Node* current = openSet.top();
46+
openSet.pop();
47+
48+
if (current->x == goal.first && current->y == goal.second) {
49+
// Reconstruct path
50+
vector<pair<int,int>> path;
51+
while (current) {
52+
path.push_back({current->x, current->y});
53+
current = current->parent;
54+
}
55+
reverse(path.begin(), path.end());
56+
return path;
57+
}
58+
59+
if (closedSet[current->x][current->y]) continue;
60+
closedSet[current->x][current->y] = true;
61+
62+
for (int i = 0; i < 4; i++) {
63+
int nx = current->x + dx[i];
64+
int ny = current->y + dy[i];
65+
66+
if (isValid(nx, ny, rows, cols, grid) && !closedSet[nx][ny]) {
67+
int g = current->gCost + 1;
68+
int h = heuristic(nx, ny, goal.first, goal.second);
69+
Node* neighbor = new Node(nx, ny, g, h, current);
70+
openSet.push(neighbor);
71+
}
72+
}
73+
}
74+
75+
return {}; // No path found
76+
}
77+
78+
// Example usage
79+
int main() {
80+
vector<vector<int>> grid = {
81+
{0, 1, 0, 0, 0},
82+
{0, 1, 0, 1, 0},
83+
{0, 0, 0, 1, 0},
84+
{0, 1, 1, 0, 0},
85+
{0, 0, 0, 0, 0}
86+
};
87+
88+
pair<int,int> start = {0, 0};
89+
pair<int,int> goal = {4, 4};
90+
91+
vector<pair<int,int>> path = aStar(grid, start, goal);
92+
93+
if (!path.empty()) {
94+
cout << "Path found:\n";
95+
for (auto p : path) {
96+
cout << "(" << p.first << "," << p.second << ") ";
97+
}
98+
cout << endl;
99+
} else {
100+
cout << "No path found.\n";
101+
}
102+
103+
return 0;
104+
}

0 commit comments

Comments
 (0)