A搜索算法(A Search Algorithm)是一种启发式搜索算法,用于在图形或网络中找到最短路径。它结合了广度优先搜索和贪婪算法的特点,通过评估每个节点的代价函数来决定下一步的移动。A搜索算法用于寻找最短路径问题,例如在地图上找到两个地点之间的最短路径,或在游戏中找到角色移动的最佳路径。
import java.util.*;
class Node {
int x, y; // 节点的坐标
int f, g, h; // f = g + h
Node parent; // 父节点
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
public class AStarSearch {
private int[][] map; // 地图
private int startX, startY; // 起始点坐标
private int endX, endY; // 终点坐标
private PriorityQueue openList; // 开放列表
private Set closedSet; // 封闭列表
public AStarSearch(int[][] map, int startX, int startY, int endX, int endY) {
this.map = map;
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
openList = new PriorityQueue<>(Comparator.comparingInt(node -> node.f));
closedSet = new HashSet<>();
}
public List findPath() {
Node startNode = new Node(startX, startY);
startNode.g = 0;
startNode.h = calculateH(startX, startY);
startNode.f = startNode.g + startNode.h;
openList.offer(startNode);
while (!openList.isEmpty()) {
Node currentNode = openList.poll();
closedSet.add(currentNode);
if (currentNode.x == endX && currentNode.y == endY) {
return getPath(currentNode);
}
List neighbors = getNeighbors(currentNode);
for (Node neighbor : neighbors) {
if (closedSet.contains(neighbor)) {
continue;
}
int g = currentNode.g + 1; // 相邻节点的移动代价为1
if (!openList.contains(neighbor) || g < neighbor.g) {
neighbor.g = g;
neighbor.h = calculateH(neighbor.x, neighbor.y);
neighbor.f = neighbor.g + neighbor.h;
neighbor.parent = currentNode;
if (!openList.contains(neighbor)) {
openList.offer(neighbor);
}
}
}
}
return null; // 未找到路径
}
private int calculateH(int x, int y) {
// 使用距离绝对值作为启发函数
return Math.abs(x - endX) + Math.abs(y - endY);
}
private List getNeighbors(Node node) {
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
List neighbors = new ArrayList<>();
for (int[] direction : directions) {
int newX = node.x + direction[0];
int newY = node.y + direction[1];
if (newX >= 0 && newX < map.length && newY >= 0 && newY < map[0].length && map[newX][newY] == 0) {
neighbors.add(new Node(newX, newY));
}
}
return neighbors;
}
private List getPath(Node endNode) {
List path = new ArrayList<>();
Node currentNode = endNode;
while (currentNode != null) {
path.add(0, currentNode);
currentNode = currentNode.parent;
}
return path;
}
public static void main(String[] args) {
int[][] map = {
{0, 0, 0, 0, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 0, 0}
};
int startX = 0;
int startY = 0;
int endX = 4;
int endY = 4;
AStarSearch astar = new AStarSearch(map, startX, startY, endX, endY);
List path = astar.findPath();
if (path != null) {
for (Node node : path) {
System.out.println("(" + node.x + ", " + node.y + ")");
}
} else {
System.out.println("未找到路径");
}
}
}