java a*算法 请写注释
时间: 2023-12-23 18:02:11 浏览: 23
好的,下面是 Java 实现 A* 算法的代码注释:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.PriorityQueue;
public class AStar {
//定义一个Node类表示每一个节点
private static class Node implements Comparable<Node> {
int x, y; //节点的坐标
int g; //从起点到当前节点的实际距离
int h; //从当前节点到终点的预计距离
Node parent; //当前节点的父节点
public Node(int x, int y) {
this.x = x;
this.y = y;
}
//计算当前节点的 f 值
public int getF() {
return g + h;
}
//比较两个节点的 f 值
@Override
public int compareTo(Node other) {
return Integer.compare(this.getF(), other.getF());
}
//判断两个节点是否相同
@Override
public boolean equals(Object obj) {
if (obj instanceof Node) {
Node other = (Node)obj;
return this.x == other.x && this.y == other.y;
}
return false;
}
//计算当前节点到终点的预计距离
public void calcH(Node end) {
this.h = Math.abs(end.x - this.x) + Math.abs(end.y - this.y);
}
}
//定义地图的宽度和高度
private static final int WIDTH = 10;
private static final int HEIGHT = 10;
//定义地图中的障碍物
private static final int[][] MAP = new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
//判断一个节点是否在地图内
private static boolean isValid(int x, int y) {
return x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT;
}
//判断一个节点是否为障碍物
private static boolean isObstacle(int x, int y) {
return MAP[y][x] == 1;
}
//查找周围可通过的节点
private static ArrayList<Node> getNeighbors(Node node) {
ArrayList<Node> neighbors = new ArrayList<>();
int x = node.x;
int y = node.y;
int[][] directions = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; //四个方向
for (int[] direction : directions) {
int dx = direction[0];
int dy = direction[1];
int nx = x + dx;
int ny = y + dy;
if (isValid(nx, ny) && !isObstacle(nx, ny)) {
neighbors.add(new Node(nx, ny));
}
}
return neighbors;
}
//计算从起点到终点的最短路径
private static ArrayList<Node> findPath(Node start, Node end) {
PriorityQueue<Node> openList = new PriorityQueue<>(); //开放列表
HashSet<Node> closeList = new HashSet<>(); //关闭列表
start.g = 0;
start.calcH(end);
openList.add(start);
while (!openList.isEmpty()) {
Node current = openList.poll(); //取出 f 值最小的节点
if (current.equals(end)) { //找到了终点
ArrayList<Node> path = new ArrayList<>();
while (current != null) {
path.add(current);
current = current.parent;
}
Collections.reverse(path); //反转路径
return path;
}
closeList.add(current); //将当前节点加入关闭列表
for (Node neighbor : getNeighbors(current)) { //遍历邻居节点
if (closeList.contains(neighbor)) { //如果邻居节点已在关闭列表中,则跳过
continue;
}
int tentativeG = current.g + 1; //计算从起点到邻居节点的实际距离
if (!openList.contains(neighbor) || tentativeG < neighbor.g) { //如果邻居节点不在开放列表中,或者当前路径更短
neighbor.g = tentativeG;
neighbor.calcH(end);
neighbor.parent = current; //更新邻居节点的父节点
if (!openList.contains(neighbor)) { //如果邻居节点不在开放列表中,则加入开放列表
openList.add(neighbor);
}
}
}
}
return null; //找不到路径
}
//测试
public static void main(String[] args) {
Node start = new Node(0, 0);
Node end = new Node(9, 9);
ArrayList<Node> path = findPath(start, end);
if (path != null) {
for (Node node : path) {
System.out.println(node.x + "," + node.y);
}
} else {
System.out.println("No path found");
}
}
}
```
希望这份注释能够帮助你理解 A* 算法的实现过程。