package FindPath;
import java.awt.Point;
import java.util.LinkedList;
import java.util.List;
public class PathSearch {
public static List<Point> findPath(int[][] map, Point start, Point goal) {
// 转换成node
StarNode startNode = new StarNode(start);
StarNode goalNode = new StarNode(goal);
// open list
LinkedList<StarNode> open = new LinkedList<StarNode>();
LinkedList<StarNode> close = new LinkedList<StarNode>();
// init
startNode.searchParent = null;
open.add(startNode);
close.add(startNode);
while (!open.isEmpty()) {
StarNode node = open.removeFirst(); //将open中第一个成员删掉,并返回给node
if (node.equals(goalNode)) { //判断取出的点是否是Destination
return construct(node); //若是,则调用construct()方法,返回一个LinkedList
} else { //若否,则执行添加节点操作如下
for (StarNode n : node.getNeighbors()) {
if (!open.contains(n) && !close.contains(n) && n.isHit(map)) { //在node的neighbors()中,
n.searchParent = node; //若open,close中均不含有该点
open.add(n); //而且该点上没有障碍物则将该点作为基点
close.add(n); //并且在open和close中都添加上该节点
}
}
}
}
return null;
}
public static List<Point> construct(StarNode node) {
LinkedList<Point> path = new LinkedList<Point>();
while (node != null) {
path.addFirst(node.location);
node = node.searchParent;
}
return path;
}
public static void main(String[] args) {
int[][] map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
List<Point> list = findPath(map, new Point(5, 1), new Point(10, 13));
for (Point p : list) {
try {
Thread.sleep(300);
}catch(InterruptedException e){}
System.out.println(p.x + "," + p.y);
}
}
}
基于JAVA的迷宫自动寻路算法实现
3星 · 超过75%的资源 需积分: 20 104 浏览量
2011-12-20
21:21:51
上传
评论 3
收藏 13KB RAR 举报
Fly_liu88
- 粉丝: 1
- 资源: 7
最新资源
- 使用Dockerfile配置Springboot应用服务发布Docker镜像-源码
- 万维考试系统-C语言题库-含题目-程序设计题导出-113道.doc
- Alibaba SpringCloud集成Nacos、openFeign实现负载均衡-源码
- 用户研究系列之Persona介绍(用户画像)
- Image_1715110604427.jpg
- mYzh..trashed-1716951334-.jpg
- Alibaba SpringCloud集成nacos实现配置中心
- 那些好的不像话的心流体验
- 基于pytorch的卷积神经网络识别是否为奥特曼的项目python源码+文档说明+数据集(课程设计)
- 基于卷积神经网络的图像风格迁移python源码+文档说明+界面图片(课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈