-
Notifications
You must be signed in to change notification settings - Fork 178
/
AStar.java
75 lines (61 loc) · 2.09 KB
/
AStar.java
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
import java.util.*;
class AStar {
class Node {
int x, y;
int g; // Cost from start
int h; // Heuristic cost to goal
Node parent;
Node(int x, int y, int g, int h, Node parent) {
this.x = x;
this.y = y;
this.g = g;
this.h = h;
this.parent = parent;
}
int f() {
return g + h; // Total cost
}
}
public List<Node> aStar(Node start, Node goal) {
PriorityQueue<Node> openSet = new PriorityQueue<>(Comparator.comparingInt(Node::f));
Set<Node> closedSet = new HashSet<>();
openSet.add(start);
while (!openSet.isEmpty()) {
Node current = openSet.poll();
if (current.equals(goal)) {
return reconstructPath(current);
}
closedSet.add(current);
for (Node neighbor : getNeighbors(current)) {
if (closedSet.contains(neighbor)) continue;
int tentativeG = current.g + 1; // Assuming uniform cost for neighbors
if (tentativeG < neighbor.g) {
neighbor.parent = current;
neighbor.g = tentativeG;
neighbor.h = heuristic(neighbor, goal);
if (!openSet.contains(neighbor)) {
openSet.add(neighbor);
}
}
}
}
return Collections.emptyList(); // No path found
}
// Define your heuristic function and getNeighbors method
private int heuristic(Node a, Node b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
private List<Node> getNeighbors(Node node) {
// Return a list of neighbor nodes (implement this based on your grid or graph structure)
return new ArrayList<>();
}
private List<Node> reconstructPath(Node node) {
List<Node> path = new ArrayList<>();
while (node != null) {
path.add(node);
node = node.parent;
}
Collections.reverse(path);
return path;
}
}