A*路径搜索问题的处理(Java)
创始人
2024-07-21 11:40:55
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*路径搜索算

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...