A*路径搜索问题的处理(Java)
创始人
2024-05-07 22:23:15
0

A路径搜索是一种常用的寻路算法,可以在图形化地图中找到最短路径。以下是一个用Java实现A路径搜索的示例代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

class Node implements Comparable {
    int x, y;
    int g, h;
    Node parent;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int f() {
        return g + h;
    }

    @Override
    public int compareTo(Node other) {
        return Integer.compare(this.f(), other.f());
    }
}

public class AStarPathfinding {
    private int[][] map;
    private int startX, startY;
    private int targetX, targetY;
    private List openList;
    private List closedList;

    public AStarPathfinding(int[][] map, int startX, int startY, int targetX, int targetY) {
        this.map = map;
        this.startX = startX;
        this.startY = startY;
        this.targetX = targetX;
        this.targetY = targetY;
        this.openList = new ArrayList<>();
        this.closedList = new ArrayList<>();
    }

    public List findPath() {
        Node startNode = new Node(startX, startY);
        Node targetNode = new Node(targetX, targetY);
        openList.add(startNode);

        while (!openList.isEmpty()) {
            Node currentNode = openList.remove(0);
            closedList.add(currentNode);

            if (currentNode.x == targetX && currentNode.y == targetY) {
                return reconstructPath(currentNode);
            }

            List neighbors = getNeighbors(currentNode);
            for (Node neighbor : neighbors) {
                if (closedList.contains(neighbor)) {
                    continue;
                }

                int cost = currentNode.g + 1;
                boolean isBetter = false;

                if (!openList.contains(neighbor)) {
                    openList.add(neighbor);
                    neighbor.h = heuristic(neighbor, targetNode);
                    isBetter = true;
                } else if (cost < neighbor.g) {
                    isBetter = true;
                }

                if (isBetter) {
                    neighbor.parent = currentNode;
                    neighbor.g = cost;
                }
            }

            Collections.sort(openList);
        }

        return null;
    }

    private List getNeighbors(Node node) {
        List neighbors = new ArrayList<>();
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        for (int[] dir : directions) {
            int neighborX = node.x + dir[0];
            int neighborY = node.y + dir[1];

            if (isValidCoordinate(neighborX, neighborY) && map[neighborX][neighborY] == 0) {
                neighbors.add(new Node(neighborX, neighborY));
            }
        }

        return neighbors;
    }

    private boolean isValidCoordinate(int x, int y) {
        return x >= 0 && x < map.length && y >= 0 && y < map[0].length;
    }

    private int heuristic(Node node1, Node node2) {
        return Math.abs(node1.x - node2.x) + Math.abs(node1.y - node2.y);
    }

    private List reconstructPath(Node node) {
        List path = new ArrayList<>();
        Node currentNode = node;

        while (currentNode != null) {
            path.add(currentNode);
            currentNode = currentNode.parent;
        }

        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args) {
        int[][] map = {
                {0, 0, 0, 0, 0},
                {0, 1, 0, 1, 0},
                {0, 1, 0, 1, 0},
                {0, 0, 0, 0, 0},
        };
        int startX = 0, startY = 0;
        int targetX = 3, targetY = 4;

        AStarPathfinding pathfinding = new AStarPathfinding(map, startX, startY, targetX, targetY);
        List path = pathfinding.findPath();

        if (path != null) {
            for (Node node : path) {
                System.out.println("(" + node.x + ", " + node.y + ")");
            }
        } else {
            System.out.println("No path found");
        }
    }
}

上述代码实现了一个简单的A*路径搜索算

相关内容

热门资讯

玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
别人打电话听不见我说话怎么回事... 当我们在使用手机时,可能会遇到别人打电话过来听不见声音的情况,这种情况可能是由多种原因导致的,下面我...
闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
frp内网穿透配置 HTTP ... HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http...
广电4k机顶盒怎么连接 广电网... 四广电网络,即四家主流的广播电视网络运营商,包括中国电信、中国移动、中国联通和中国广电,这些运营商为...
hwid是永久激活吗 hwid... HWID,全称Hardware ID,是硬件识别码的缩写,它是计算机硬件制造商为了区分每一台设备而分...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...