淮安阿里云代理商:a*算法java源码

下面是使用 Java 实现 A* 算法的示例代码:

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

class Node implements Comparable<Node> {
    private int x;
    private int y;
    private int f; // f(n) = g(n) + h(n)
    private int g; // 到起点的实际代价
    private int h; // 到目标点的估计代价
    private Node parent;

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

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getF() {
        return f;
    }

    public void setF(int f) {
        this.f = f;
    }

    public int getG() {
        return g;
    }

    public void setG(int g) {
        this.g = g;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

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

public class AStarAlgorithm {
    private static final int[][] DIRECTIONS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    private int[][] grid;
    private int numRows;
    private int numCols;
    private Node startNode;
    private Node endNode;

    public AStarAlgorithm(int[][] grid) {
        this.grid = grid;
        this.numRows = grid.length;
        this.numCols = grid[0].length;
    }

    public List<Node> findPath(int startX, int startY, int endX, int endY) {
        startNode = new Node(startX, startY);
        endNode = new Node(endX, endY);

        PriorityQueue<Node> openSet = new PriorityQueue<>();
        Set<Node> closedSet = new HashSet<>();

        startNode.setG(0);
        startNode.setH(manhattanDistance(startNode, endNode));
        startNode.setF(startNode.getG() + startNode.getH());

        openSet.add(startNode);

        while (!openSet.isEmpty()) {
            Node currNode = openSet.poll();

            if (currNode.equals(endNode)) {
                return reconstructPath(currNode);
            }

            closedSet.add(currNode);

            for (int[] direction : DIRECTIONS) {
                int newX = currNode.getX() + direction[0];
                int newY = currNode.getY() + direction[1];

                if (isValid(newX, newY) && !closedSet.contains(new Node(newX, newY))) {
                    Node neighbor = new Node(newX, newY);

                    int tentativeG = currNode.getG() + 1;

                    if (!openSet.contains(neighbor) || tentativeG < neighbor.getG()) {
                        neighbor.setParent(currNode);
                        neighbor.setG(tentativeG);
                        neighbor.setH(manhattanDistance(neighbor, endNode));
                        neighbor.setF(neighbor.getG() + neighbor.getH());

                        if (!openSet.contains(neighbor)) {
                            openSet.add(neighbor);
                        }
                    }
                }
            }
        }

        return null;
    }

    private int manhattanDistance(Node node1, Node node2) {
        return Math.abs(node1.getX() - node2.getX()) + Math.abs(node1.getY() - node2.getY());
    }

    private boolean isValid(int x, int y) {
        return x >= 0 && x < numRows && y >= 0 && y < numCols && grid[x][y] != 1;
    }

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

        while (node != null) {
            path.add(node);
            node = node.getParent();
        }

        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args) {
        int[][] grid = {
            {0, 0, 0, 1, 0},
            {1, 1, 0, 1, 0},
            {0, 0, 0, 0, 0},
            {0, 1, 1, 1, 1},
            {0, 0, 0, 0, 0}
        };

        AStarAlgorithm astar = new AStarAlgorithm(grid);
        List<Node> path = astar.findPath(0, 0, 4, 4);

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

这个示例代码使用 A* 算法实现了从起点到终点的路径搜索。在 findPath 方法中,使用优先队列 openSet 存储待探索的节点,使用集合 closedSet 存储已经探索过的节点。通过计算节点的 f(n) 值(f(n) = g(n) + h(n))来评估节点的优先级,优先探索具有较小 f(n) 值的节点。

注意:此示例代码中的 grid 是一个二维数组,其中 0 表示可以通过的路径,1 表示障碍物。将起点和终点的坐标传递给 findPath 方法,将返回路径上的所有节点。如果找不到路径,返回 null

请注意,这只是 A* 算法的一种实现方式,还有其他的实现方式可供选择。

淮安阿里云代理商:a*算法java源码

下面是一个使用A*算法的Java源码示例:

import java.util.*;

class Node implements Comparable<Node> {
    int x, y; // 节点的坐标
    int g, h; // 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 o) {
        return Integer.compare(this.f(), o.f());
    }
}

class AStar {
    private int[][] grid;
    private int rows, cols;
    private Node startNode, endNode;

    public AStar(int[][] grid, int rows, int cols, int startX, int startY, int endX, int endY) {
        this.grid = grid;
        this.rows = rows;
        this.cols = cols;
        startNode = new Node(startX, startY);
        endNode = new Node(endX, endY);
    }

    public List<Node> findPath() {
        List<Node> openSet = new ArrayList<>();
        Set<Node> closedSet = new HashSet<>();
        openSet.add(startNode);

        while (!openSet.isEmpty()) {
            Node currentNode = openSet.get(0);

            // 寻找f值最小的节点
            for (int i = 1; i < openSet.size(); i++) {
                if (openSet.get(i).f() < currentNode.f()) {
                    currentNode = openSet.get(i);
                }
            }

            openSet.remove(currentNode);
            closedSet.add(currentNode);

            if (currentNode.x == endNode.x && currentNode.y == endNode.y) {
                return buildPath(currentNode);
            }

            List<Node> neighbors = getNeighbors(currentNode);

            for (Node neighbor : neighbors) {
                if (closedSet.contains(neighbor)) {
                    continue;
                }

                int newG = currentNode.g + 1;
                boolean isOpenSetContainsNeighbor = openSet.contains(neighbor);

                if (!isOpenSetContainsNeighbor || newG < neighbor.g) {
                    neighbor.g = newG;
                    neighbor.h = manhattanDistance(neighbor, endNode);
                    neighbor.parent = currentNode;

                    if (!isOpenSetContainsNeighbor) {
                        openSet.add(neighbor);
                    }
                }
            }
        }

        return null;
    }

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

    private boolean isValidPosition(int x, int y) {
        return x >= 0 && x < rows && y >= 0 && y < cols && grid[x][y] == 0;
    }

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

        for (int i = 0; i < 4; i++) {
            int newX = node.x + dx[i];
            int newY = node.y + dy[i];

            if (isValidPosition(newX, newY)) {
                neighbors.add(new Node(newX, newY));
            }
        }

        return neighbors;
    }

    private List<Node> buildPath(Node endNode) {
        List<Node> path = new ArrayList<>();
        Node currentNode = endNode;

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

        return path;
    }
}

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        int cols = 5;
        int[][] grid = {
            {0, 0, 0, 0, 0},
            {0, 1, 1, 1, 0},
            {0, 1, 0, 0, 0},
            {0, 1, 0, 1, 1},
            {0, 0, 0, 0, 0}
        };

        int startX = 0;
        int startY = 0;
        int endX = 4;
        int endY = 4;

        AStar aStar = new AStar(grid, rows, cols, startX, startY, endX, endY);
        List<Node> path = aStar.findPath();

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

这个示例代码实现了一个迷宫路径查找的问题。grid数组表示迷宫地图,0表示可以通过的通路,1表示障碍物。startX和startY是起点的坐标,endX和endY是终点的坐标。程序会使用A*算法查找从起点到终点的最短路径,并将路径上的节点打印出来。如果没有找到路径,输出”No path found.”。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/118770.html

(0)
luotuoemo的头像luotuoemo
上一篇 2024年1月3日 04:24
下一篇 2024年1月3日 04:37

相关推荐

  • 青岛阿里云代理商:阿里云iot套件

    阿里云iot套件是阿里云提供的一款物联网云平台解决方案,旨在帮助企业快速构建和扩展物联网应用。作为青岛阿里云代理商,可以提供以下服务和支持: 产品咨询和方案设计:根据客户需求,提供针对物联网领域的行业解决方案,并进行需求分析与产品定制。 产品销售和合同签订:代理销售阿里云iot产品,提供购买合同签署服务,确保客户的合法权益。 平台部署和配置:协助客户完成阿里…

    2024年1月17日
    15400
  • 郑州阿里云代理商:asp.net 网站代码

    非常抱歉,我无法提供具体的ASP.NET网站代码,因为这需要根据具体的需求和功能来进行开发。ASP.NET是一种基于微软的.NET框架的网站开发技术,可以使用C#或VB.NET等编程语言来编写网站代码。 一般来说,ASP.NET网站的代码包括以下部分: 页面文件:ASPX文件,其中包含网页的HTML和ASP.NET控件。这些文件用于提供网站的用户界面。 代码…

    2024年1月29日
    12900
  • 阿里云智能app投资是真的吗安全吗

    阿里云智能APP投资存在风险,投资者需要明确风险,并做好风险防范和管理。传统金融市场理财产品有监管、备案等措施,但智能APP投资可能缺乏相关监管机制,投资者需要谨慎选择投资平台并对投资项目进行尽职调查。同时,阿里云作为知名云计算服务提供商,其APP投资平台的安全性较高,但仍需注意个人信息保护和账号安全等问题。综上所述,投资者应该理性对待阿里云智能APP投资,…

    2023年8月24日
    13500
  • 哈尔滨阿里云代理商:asp连接数据库实现登录

    ASP连接数据库实现登录的代码如下: 1.建立数据库连接 <!–#include file=”adovbs.inc”–><%dim conn,rsset conn=server.createobject(“adodb.connection”)conn.Open &#82…

    2024年2月28日
    12900
  • 阿里云国际站代理商:app打包服务器搭建

    如果您想成为阿里云国际站的代理商,并且需要搭建一个用于App打包的服务器,这里有一些基本的步骤和考虑因素: 注册成为代理商: 首先,您需要在阿里云的官网上申请成为代理商。这通常需要填写一些公司信息和业务详情。 选择合适的服务器产品: 阿里云提供多种服务器选项,包括ECS(弹性计算服务)和简单应用服务器等。您需要根据App打包的需求(如CPU、内存、存储空间等…

    2024年7月7日
    11600

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/