|
| 1 | +// Algorithm: A* Search |
| 2 | +// Type: Graph, Pathfinding, Heuristic-based Search |
| 3 | +// Time Complexity: O(E) or O(V log V) with priority queue |
| 4 | +// Space Complexity: O(V) |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <math.h> |
| 9 | +#include <limits.h> |
| 10 | + |
| 11 | +#define N 5 // Grid size |
| 12 | +#define INF 1000000 |
| 13 | + |
| 14 | +typedef struct { |
| 15 | + int x, y; |
| 16 | +} Point; |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + int f, g, h; |
| 20 | + Point parent; |
| 21 | + int visited; |
| 22 | +} Node; |
| 23 | + |
| 24 | +int dx[] = {-1, 1, 0, 0}; |
| 25 | +int dy[] = {0, 0, -1, 1}; |
| 26 | + |
| 27 | +// Heuristic: Manhattan distance |
| 28 | +int heuristic(Point a, Point b) { |
| 29 | + return abs(a.x - b.x) + abs(a.y - b.y); |
| 30 | +} |
| 31 | + |
| 32 | +// Check if position is inside grid and not blocked |
| 33 | +int isValid(int grid[N][N], int x, int y) { |
| 34 | + return x >= 0 && x < N && y >= 0 && y < N && grid[x][y] == 0; |
| 35 | +} |
| 36 | + |
| 37 | +// A* algorithm on grid |
| 38 | +void aStar(int grid[N][N], Point start, Point goal) { |
| 39 | + Node nodes[N][N]; |
| 40 | + |
| 41 | + for (int i = 0; i < N; ++i) |
| 42 | + for (int j = 0; j < N; ++j) { |
| 43 | + nodes[i][j].f = INF; |
| 44 | + nodes[i][j].g = INF; |
| 45 | + nodes[i][j].h = 0; |
| 46 | + nodes[i][j].visited = 0; |
| 47 | + nodes[i][j].parent = (Point){-1, -1}; |
| 48 | + } |
| 49 | + |
| 50 | + nodes[start.x][start.y].g = 0; |
| 51 | + nodes[start.x][start.y].h = heuristic(start, goal); |
| 52 | + nodes[start.x][start.y].f = nodes[start.x][start.y].h; |
| 53 | + |
| 54 | + while (1) { |
| 55 | + int minF = INF; |
| 56 | + Point current = {-1, -1}; |
| 57 | + |
| 58 | + // Find node with lowest f |
| 59 | + for (int i = 0; i < N; ++i) |
| 60 | + for (int j = 0; j < N; ++j) |
| 61 | + if (!nodes[i][j].visited && nodes[i][j].f < minF) { |
| 62 | + minF = nodes[i][j].f; |
| 63 | + current = (Point){i, j}; |
| 64 | + } |
| 65 | + |
| 66 | + if (current.x == -1) { |
| 67 | + printf("No path found\n"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (current.x == goal.x && current.y == goal.y) { |
| 72 | + // Reconstruct path |
| 73 | + printf("Path: "); |
| 74 | + Point p = goal; |
| 75 | + while (!(p.x == start.x && p.y == start.y)) { |
| 76 | + printf("(%d,%d) <- ", p.x, p.y); |
| 77 | + p = nodes[p.x][p.y].parent; |
| 78 | + } |
| 79 | + printf("(%d,%d)\n", start.x, start.y); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + nodes[current.x][current.y].visited = 1; |
| 84 | + |
| 85 | + // Explore neighbors |
| 86 | + for (int d = 0; d < 4; ++d) { |
| 87 | + int nx = current.x + dx[d]; |
| 88 | + int ny = current.y + dy[d]; |
| 89 | + |
| 90 | + if (!isValid(grid, nx, ny) || nodes[nx][ny].visited) continue; |
| 91 | + |
| 92 | + int tentativeG = nodes[current.x][current.y].g + 1; |
| 93 | + |
| 94 | + if (tentativeG < nodes[nx][ny].g) { |
| 95 | + nodes[nx][ny].parent = current; |
| 96 | + nodes[nx][ny].g = tentativeG; |
| 97 | + nodes[nx][ny].h = heuristic((Point){nx, ny}, goal); |
| 98 | + nodes[nx][ny].f = tentativeG + nodes[nx][ny].h; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Example usage |
| 105 | +int main() { |
| 106 | + int grid[N][N] = { |
| 107 | + {0, 1, 0, 0, 0}, |
| 108 | + {0, 1, 0, 1, 0}, |
| 109 | + {0, 0, 0, 1, 0}, |
| 110 | + {1, 1, 0, 0, 0}, |
| 111 | + {0, 0, 0, 1, 0} |
| 112 | + }; |
| 113 | + |
| 114 | + Point start = {0, 0}; |
| 115 | + Point goal = {4, 4}; |
| 116 | + |
| 117 | + aStar(grid, start, goal); |
| 118 | + return 0; |
| 119 | +} |
0 commit comments