java 实现a*算法定义邻居节点只包含上下左右四个方向

时间: 2024-05-05 07:21:12 浏览: 5
好的,下面是 Java 实现 A* 算法,定义邻居节点只包含上下左右四个方向的示例代码: ```java import java.util.*; public class AStar { private static final int[][] DIRECTIONS = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 上下左右四个方向 private PriorityQueue<Node> openSet; // 存放未探索的节点 private Set<Node> closedSet; // 存放已探索的节点 private int[][] grid; // 地图,0 表示可以通过,1 表示障碍物 private int startRow; // 起点的行号 private int startCol; // 起点的列号 private int endRow; // 终点的行号 private int endCol; // 终点的列号 public AStar(int[][] grid, int startRow, int startCol, int endRow, int endCol) { this.openSet = new PriorityQueue<>(Comparator.comparingInt(node -> node.f)); this.closedSet = new HashSet<>(); this.grid = grid; this.startRow = startRow; this.startCol = startCol; this.endRow = endRow; this.endCol = endCol; } public List<Node> search() { Node startNode = new Node(startRow, startCol); startNode.g = 0; startNode.h = heuristic(startRow, startCol); startNode.f = startNode.g + startNode.h; openSet.offer(startNode); while (!openSet.isEmpty()) { Node currentNode = openSet.poll(); if (currentNode.row == endRow && currentNode.col == endCol) { return getPath(currentNode); } closedSet.add(currentNode); for (int[] direction : DIRECTIONS) { int newRow = currentNode.row + direction[0]; int newCol = currentNode.col + direction[1]; if (newRow < 0 || newRow >= grid.length || newCol < 0 || newCol >= grid[0].length || grid[newRow][newCol] == 1) { continue; } Node neighbor = new Node(newRow, newCol); if (closedSet.contains(neighbor)) { continue; } int tentativeGScore = currentNode.g + 1; if (!openSet.contains(neighbor)) { neighbor.h = heuristic(newRow, newCol); openSet.offer(neighbor); } else if (tentativeGScore >= neighbor.g) { continue; } neighbor.parent = currentNode; neighbor.g = tentativeGScore; neighbor.f = neighbor.g + neighbor.h; } } return null; } private List<Node> getPath(Node node) { List<Node> path = new ArrayList<>(); Node currentNode = node; while (currentNode != null) { path.add(currentNode); currentNode = currentNode.parent; } Collections.reverse(path); return path; } private int heuristic(int row, int col) { return Math.abs(row - endRow) + Math.abs(col - endCol); } private static class Node { int row; int col; int f; // f = g + h int g; // 起点到该节点的距离 int h; // 该节点到终点的估计距离 Node parent; Node(int row, int col) { this.row = row; this.col = col; } @Override public int hashCode() { return Objects.hash(row, col); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Node node = (Node) obj; return row == node.row && col == node.col; } } } ``` 使用时只需传入地图、起点和终点的坐标,调用 `search` 方法即可得到一条从起点到终点的最短路径。例如: ```java int[][] grid = { {0, 0, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}, {0, 1, 0, 0} }; int startRow = 0; int startCol = 0; int endRow = 5; int endCol = 3; AStar aStar = new AStar(grid, startRow, startCol, endRow, endCol); List<AStar.Node> path = aStar.search(); if (path != null) { for (AStar.Node node : path) { System.out.println("(" + node.row + ", " + node.col + ")"); } } else { System.out.println("No path found."); } ```

相关推荐

最新推荐

recommend-type

Java编程实现A*算法完整代码

主要介绍了Java编程实现A*算法完整代码,简单介绍了a星算法,然后分享了完整测试代码,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

【WHUT】*实验报告*《人工智能概论》课内实验:A*算法仿真实验

A*算法仿真实验 请下载并安装附件(虚拟实验软件-启发式搜索.rar)里的智能搜索算法教学实验系统,然后点击A*算法进行仿真实验。 实验要求如下: 1. 单击"A*算法介绍",回顾A*算法的基本原理。 2. 在"A*算法演示...
recommend-type

Python3 A*寻路算法实现方式

今天小编就为大家分享一篇Python3 A*寻路算法实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

C++使用递归和非递归算法实现的二叉树叶子节点个数计算方法

主要介绍了C++使用递归和非递归算法实现的二叉树叶子节点个数计算方法,涉及C++二叉树的定义、遍历、统计相关操作技巧,需要的朋友可以参考下
recommend-type

基于java实现的ECC加密算法示例

主要介绍了基于java实现的ECC加密算法,简单说明了ECC算法的概念、原理,并结合实例形式分析了java实现ECC加密算法的定义与使用技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。