A*—java代码
import java.util.ArrayList; // A*算法寻路
public class AStar2 {
public static final int[][] maps = {
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
}; public static int straight = 10;
public static int diagonal = 14; // 开放列表
public static ArrayList<Node> openList = new ArrayList<>();
// 闭合列表
public static ArrayList<Node> colseList = new ArrayList<>();
// 方向
public static int[][] direct = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; public static void main(String[] args) {
//定点:起点终点
Node start = new Node(5, 1);
Node end = new Node(5, 4); Node endNode = findPath(start, end); printMap(maps, start, end); ArrayList<Node> arrayList = endNode != null ? getPaths(endNode) : null; printPaths(arrayList); } // 从起点开始,找到到终点的一条最短路径
private static Node findPath(Node start, Node end) {
start.G = 0;
openList.add(start); while (!openList.isEmpty()) {
//从开放列表中拿到最小F节点
Node cureNode = minFINOpenList(openList);
openList.remove(cureNode);
// 将该节点加入到闭合列表中
colseList.add(cureNode); // 当前节点的全部合法邻居
ArrayList<Node> neighbors = getNeighbor(cureNode);
for (Node nbrNode : neighbors) {
// 邻居已经在openList
if (exists(openList, nbrNode) != null)
updateG(cureNode, nbrNode);
// 邻居不在openList
else joinOpenList(cureNode, nbrNode, end);
}
if (exists(openList, end) != null)
return exists(openList, end);
} return null;
} private static ArrayList<Node> getPaths(Node endNode) {
ArrayList<Node> arrayList = new ArrayList<>();
Node parent = endNode;
while (parent != null) {
arrayList.add(parent);
parent = parent.parent;
}
return arrayList;
} private static int calStep(Node node, Node cur) {
if (inLine(node, cur))
return straight;
else return diagonal;
} private static int calH(Node endNode, Node nbrNode) {
return Math.abs(endNode.y - nbrNode.y) + Math.abs(endNode.x - nbrNode.x);
} // 计算距离起点的距离
private static int calG(Node cureNode, Node nbrNode) {
int step = calStep(cureNode, nbrNode);
return cureNode.G + step;
} private static boolean inLine(Node nbr, Node cur) {
if (nbr.x == cur.x || nbr.y == cur.y)
return true;
return false;
} // 途径当前节点到达节点node的路径G会不会更短
private static void updateG(Node cureNode, Node nbrNode) {
int step = calStep(cureNode, nbrNode);
int G = calG(cureNode, nbrNode);
if (G < nbrNode.G) {
nbrNode.G = G;
nbrNode.parent = cureNode;
nbrNode.calcF();
}
} private static void joinOpenList(Node curNode, Node nbrNode, Node endNode) {
openList.add(nbrNode);
nbrNode.parent = curNode;
nbrNode.G = calG(curNode, nbrNode);
nbrNode.H = calH(endNode, nbrNode);
nbrNode.calcF();
} // 达到当前节点的可达,且不在closeList中的邻居节点
private static ArrayList<Node> getNeighbor(Node cureNode) {
ArrayList<Node> arrayList = new ArrayList<>();
//从当前节点想八个方向扩散
for (int i = 0; i < 8; i++) {
int newRow = cureNode.x + direct[i][0];
int newCol = cureNode.y + direct[i][1];
//当前邻居节点: 可达、不在closeList中
if (isAccesse(newRow, newCol) && !exists(colseList, newRow, newCol)) {
arrayList.add(new Node(newRow, newCol));
}
}
return arrayList;
} private static Node exists(ArrayList<Node> colseList, Node cur) {
for (Node node : colseList) {
if (node.x == cur.x && node.y == cur.y)
return node;
}
return null;
} private static boolean exists(ArrayList<Node> colseList, int newX, int newY) {
for (Node node : colseList) {
if (node.x == newX && node.y == newY)
return true;
}
return false;
} // 可达性分析(非障碍物)
private static boolean isAccesse(int newX, int newY) {
if (0 <= newX && newX < maps.length && 0 <= newY && newY < maps[0].length)
return maps[newX][newY] == 0;
return false;
} // 从开放列表中找到最小F=G+H的节点
private static Node minFINOpenList(ArrayList<Node> openList) {
Node min = openList.get(0);
for (Node node : openList) {
if (node.F < min.F)
min = node;
}
return min;
} private static void printMap(int[][] maps, Node start, Node end) { for (int col = 0; col < maps[0].length; col++) {
System.out.print("\t" + col + "");
}
System.out.print("\n-----------------------------------------\n");
int count = 0;
for (int row = 0; row < maps.length; row++) {
for (int col = 0; col < maps[0].length; col++) {
if (col == 0)
System.out.print(count++ + "|\t");
if (row == start.x && col == start.y || row == end.x && col == end.y)
System.out.print("X\t");
else
System.out.print(maps[row][col] + "\t");
}
System.out.println();
}
System.out.println();
} public static void printPaths(ArrayList<Node> arrayList) {
if (arrayList == null) {
System.out.println("无路可走");
return;
} // 地图形式
for (int col = 0; col < maps[0].length; col++) {
System.out.print("\t" + col + "");
}
System.out.print("\n-----------------------------------------\n");
int count = 0; for (int row = 0; row < maps.length; row++) {
for (int col = 0; col < maps[0].length; col++) {
if (col == 0)
System.out.print(count++ + "|\t");
if (exists(arrayList, row, col)) {
System.out.print("X\t");
} else {
System.out.print(maps[row][col] + "\t");
} }
System.out.println();
}
System.out.println();
// 路径形式
for (int i = arrayList.size() - 1; i >= 0; i--) {
if (i == 0)
System.out.print(arrayList.get(i));
else
System.out.print(arrayList.get(i) + "->");
}
System.out.println();
} }
结果
0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 0 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 0 1 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 1 0 0 0 0 0 0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 X 0 0 0 0 0
3| 0 0 X 1 X 0 0 0 0
4| 0 X 0 1 X 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 0 1 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 1 0 0 0 0 0 (5,1)->(4,1)->(3,2)->(2,3)->(3,4)->(4,4)->(5,4)
0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 0 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 0 X 0 0 0 0
6| 0 0 0 0 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 0 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X X X X 0 0 0 0
6| 0 0 0 0 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 (5,1)->(5,2)->(5,3)->(5,4)
0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 0 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 0 1 0 0 0 0 0
7| 0 0 0 0 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 0 0 0 0 0 0
1| 0 0 0 0 0 0 0 0 0
2| 0 0 0 0 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 X 1 X 0 0 0 0
7| 0 0 0 X 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 (5,1)->(6,2)->(7,3)->(6,4)->(5,4)
0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 1 0 0 0 0 0
1| 0 0 0 1 0 0 0 0 0
2| 0 0 0 1 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 0 1 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 1 0 0 0 0 0 无路可走
0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 1 0 0 0 0 0
1| 0 0 0 1 0 0 0 0 0
2| 0 0 0 1 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 0 0 1 0 0 0 0 0
7| 0 0 0 1 0 0 0 0 0
8| 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
-----------------------------------------
0| 0 0 0 1 0 0 0 0 0
1| 0 0 0 1 0 0 0 0 0
2| 0 0 0 1 0 0 0 0 0
3| 0 0 0 1 0 0 0 0 0
4| 0 0 0 1 0 0 0 0 0
5| 0 X 0 1 X 0 0 0 0
6| 0 X 0 1 X 0 0 0 0
7| 0 0 X 1 X 0 0 0 0
8| 0 0 0 X 0 0 0 0 0 (5,1)->(6,1)->(7,2)->(8,3)->(7,4)->(6,4)->(5,4)
A*—java代码的更多相关文章
- 对一致性Hash算法,Java代码实现的深入研究
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
- 怎样编写高质量的java代码
代码质量概述 怎样辨别一个项目代码写得好还是坏?优秀的代码和腐化的代码区别在哪里?怎么让自己写的代码既漂亮又有生命力?接下来将对代码质量的问题进行一些粗略的介绍.也请有过代码质量相关经验的朋友 ...
- 数据结构笔记--二叉查找树概述以及java代码实现
一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...
- java代码的初始化过程研究
刚刚在ITeye上看到一篇关于java代码初始化的文章,看到代码我试着推理了下结果,虽然是大学时代学的知识了,没想到还能做对.(看来自己大学时掌握的基础还算不错,(*^__^*) 嘻嘻……)但 ...
- JDBC——Java代码与数据库链接的桥梁
常用数据库的驱动程序及JDBC URL: Oracle数据库: 驱动程序包名:ojdbc14.jar 驱动类的名字:oracle.jdbc.driver.OracleDriver JDBC URL:j ...
- 利用Java代码在某些时刻创建Spring上下文
上一篇中,描述了如何使用Spring隐式的创建bean,但当我们需要引进第三方类库添加到我们的逻辑上时,@Conponent与@Autowired是无法添加到类上的,这时,自动装配便不适用了,我们需要 ...
- lombok 简化java代码注解
lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...
- 远程debug调试java代码
远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAV ...
- 几种简单的负载均衡算法及其Java代码实现
什么是负载均衡 负载均衡,英文名称为Load Balance,指由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助.通过某种负载分担技 ...
- 希尔排序及希尔排序java代码
原文链接:http://www.orlion.ga/193/ 由上图可看到希尔排序先约定一个间隔(图中是4),然后对0.4.8这个三个位置的数据进行插入排序,然后向右移一位对位置1.5.9进行插入排序 ...
随机推荐
- 【MVC】MusicStore相关资料
引言 当你对MVC的项目结构有一定了解时,那就可以开始学习一个世界级的MVC入门demo--MusicStore.学习的绝招就是把它抄一遍. 相关资料 MVC Music Store Codeple ...
- 【javascript基础】之【__lookupSetter__ 跟 __lookupSetter__】
描述: 返回getter setter所定义的函数 语法: obj.__lookupGetter__(sprop) 参数: getter setter中定义的字符串属性 注意:这不是标准的方法,ecm ...
- Docker ENTRYPOINT
entrypoint: 在启动镜像的时候会执行这个命令下的脚本,在docker run 和docker start情况下都会触发. 好比这个脚本是对某一个文件追加数据,每次start的时候都会追加,文 ...
- 如何将Virtualbox和VMware虚拟机相互转换[译文211] - 转
迁移到其他的虚拟机程序可行会吓倒一批人.如果你已经按照自己的喜好设置好了虚拟机,那么就不需要再从头安装——你可以迁移现有的虚拟机. VirtualBox 和 VMware 使用不同的虚拟机格式,不过他 ...
- angular的中文文档在这里。。
链接 仿ant-design的 angular组件 echarts文档
- BZOJ4012: [HNOI2015]开店【动态点分治】
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- GIST特征描述符使用(转)
GIST特征描述符使用 一种场景特征描述 场景特征描述? 通常的特征描述符都是对图片的局部特征进行描述的,以这种思路进行场景描述是不可行的. 比如:对于“大街上有一些行人”这个场景,我们必须通过局部特 ...
- 1013:Digital Roots
注意:大数要用字符串表示! sprintf:字符串格式化命令 主要功能:将格式化的数据写入某个字符串缓冲区 头文件:<stdio.h> 原型 int sprintf( char *buff ...
- (考研)PV操作和信号量
就绪:除了CPU其他都行了 进程的阻塞:进程因等待某事件(如等待I/O设备,等待临街资源)而暂时不能运行的状态,此时即使处理机空闲,进程也无法使用. ************************* ...
- Linux之 增加swap空间
引言 :有时候我们会遇到安装os时候,swap分区过小,导致某些大软件无法安装的问题.我们可以在linux下增大swap分区的空间. 以下的操作都要在root用户下执行: 0. 记录原 swap 分区 ...