算法之A星算法(寻路)
1.启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标。这样可以省略大量无谓的搜索路径,提高了效率。在启发式搜索中,对位置的估价是十分重要的。采用了不同的估价可以有不同的效果。
2.估价算法:从当前节点移动到目标节点的预估损耗。
预估算法有:曼哈顿(manhattan)等。
3.算法特点:理论上时间是最优的,但空间增长是指数型的。
4.java实现:上下左右移动
package cn.liushaofeng.algorithm; import java.util.ArrayList;
import java.util.List; /**
* A Star Algorithm
* @author liushaofeng
* @date 2015-8-24 下午11:05:48
* @version 1.0.0
*/
public class AstarAlgorithm
{
private List<Node> openList = null;
private List<Node> closeList = null;
private int[][] map; /**
* default constructor
* @param map data map
*/
public AstarAlgorithm(int[][] map)
{
this.map = map;
this.openList = new ArrayList<Node>();
this.closeList = new ArrayList<Node>();
} /**
* find path
* @param srcNode source node
* @param desNode destination node
* @return node path
*/
public Node findPath(Node srcNode, Node desNode)
{
init(srcNode);
do
{
if (openList.isEmpty())
{
break;
} Node node = openList.get(0);
List<Node> aroundPoint = getAroundPoint(srcNode, node, desNode);
openList.addAll(aroundPoint);
closeList.add(node);
openList.remove(node); } while (!findDes(desNode)); return findNodePath(desNode);
} private Node findNodePath(Node desNode)
{
for (Node node : openList)
{
if (node.getX() == desNode.getX() && node.getY() == desNode.getY())
{
return node;
}
}
return null;
} private boolean findDes(Node desNode)
{
for (Node node : openList)
{
if (node.getX() == desNode.getX() && node.getY() == desNode.getY())
{
return true;
}
}
return false;
} private void init(Node srcNode)
{
openList.add(srcNode);
} // top bottom left and right, four points
private List<Node> getAroundPoint(Node srcNode, Node nextNode, Node desNode)
{
int x = srcNode.getX();
int y = srcNode.getY(); int[] xData = new int[2];
int[] yData = new int[2];
if (x - 1 >= 0)
{
xData[0] = x - 1;
}
if (x + 1 < map.length)
{
xData[1] = x + 1;
} if (y - 1 >= 0)
{
yData[0] = y - 1;
}
if (y + 1 < map[0].length)
{
yData[1] = y + 1;
} List<Node> tmpList = new ArrayList<Node>(); for (int i : xData)
{
Node node = new Node(i, y, srcNode);
if (!isObstacle(node) && !inClosetList(node))
{
calcWeight(srcNode, node, desNode);
tmpList.add(node);
}
} for (int i : yData)
{
Node node = new Node(x, i, srcNode);
if (!isObstacle(node) && !inClosetList(node))
{
calcWeight(srcNode, node, desNode);
tmpList.add(node);
}
} return tmpList;
} private void calcWeight(Node parentNode, Node node, Node desNode)
{
node.setG(parentNode.getG() + 10);
int h = Math.abs(node.getX() - desNode.getX()) + Math.abs(node.getY() - desNode.getY());
node.setWeight(node.getG() + h * 10);
} private boolean inClosetList(Node nextNode)
{
for (Node node : closeList)
{
if (node.getX() == nextNode.getX() && node.getY() == nextNode.getY())
{
return true;
}
}
return false;
} private boolean isObstacle(Node nextNode)
{
return map[nextNode.getX()][nextNode.getY()] == 1;
} public static void main(String[] args)
{
int[][] map =
{
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 } }; AstarAlgorithm astar = new AstarAlgorithm(map);
Node pathNode = astar.findPath(new Node(3, 1, null), new Node(3, 5, null));
System.out.println(pathNode == null ? "Can not find path!" : pathNode.toString());
}
}
查看代码
数据模型
package cn.liushaofeng.algorithm; import java.util.ArrayList;
import java.util.List; /**
* Node
* @author liushaofeng
* @date 2015-8-24 下午09:48:53
* @version 1.0.0
*/
public class Node
{
private Node parentNode;
private int x;
private int y; private int weight;
private int g; /**
* default constructor
* @param x x point
* @param y y point
* @param parentNode parent node
*/
public Node(int x, int y, Node parentNode)
{
this.x = x;
this.y = y;
this.parentNode = parentNode;
} public int getG()
{
return g;
} public void setG(int g)
{
this.g = g;
} public Node getParentNode()
{
return parentNode;
} public void setParentNode(Node parentNode)
{
this.parentNode = parentNode;
} public int getX()
{
return x;
} public void setX(int x)
{
this.x = x;
} public int getY()
{
return y;
} public void setY(int y)
{
this.y = y;
} public int getWeight()
{
return weight;
} public void setWeight(int weight)
{
this.weight = weight;
} @Override
public String toString()
{
return getPath();
} private String getPath()
{
List<Node> dataList = new ArrayList<Node>();
Node node = this;
while (node != null)
{
dataList.add(node);
node = node.getParentNode();
} StringBuffer sb = new StringBuffer();
for (int i = dataList.size() - 1; i >= 0; i--)
{
if (i == 0)
{
sb.append("(" + dataList.get(i).getX() + "," + dataList.get(i).getY() + ")");
} else
{
sb.append("(" + dataList.get(i).getX() + "," + dataList.get(i).getY() + ")->");
}
}
return sb.toString();
}
}
查看代码
代码待调试。
算法之A星算法(寻路)的更多相关文章
- A*搜寻算法(A星算法)
A*搜寻算法[编辑] 维基百科,自由的百科全书 本条目需要补充更多来源.(2015年6月30日) 请协助添加多方面可靠来源以改善这篇条目,无法查证的内容可能会被提出异议而移除. A*搜索算法,俗称A星 ...
- 算法 A-Star(A星)寻路
一.简介 在游戏中,有一个很常见地需求,就是要让一个角色从A点走向B点,我们期望是让角色走最少的路.嗯,大家可能会说,直线就是最短的.没错,但大多数时候,A到B中间都会出现一些角色无法穿越的东西,比如 ...
- JS算法之A*(A星)寻路算法
今天写一个连连看的游戏的时候,接触到了一些寻路算法,我就大概讲讲其中的A*算法. 这个是我学习后的一点个人理解,有错误欢迎各位看官指正. 寻路模式主要有三种:广度游戏搜索.深度优先搜索和启发式搜索. ...
- 算法起步之A星算法
原文:算法起步之A星算法 用途: 寻找最短路径,优于bfs跟dfs 描述: 基本描述是,在深度优先搜索的基础上,增加了一个启发式算法,在选择节点的过程中,不是盲目选择,而是有目的的选的,F=G+H,f ...
- Cocos2d-x 3.1.1 学习日志16--A星算法(A*搜索算法)学问
A *搜索算法称为A星算法.这是一个在图形平面,路径.求出最低通过成本的算法. 经常使用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上. 首先:1.在Map地图中任取2个点,開始点和结束点 ...
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...
- Java开源-astar:A 星算法
astar A星算法Java实现 一.适用场景 在一张地图中,绘制从起点移动到终点的最优路径,地图中会有障碍物,必须绕开障碍物. 二.算法思路 1. 回溯法得到路径 (如果有路径)采用“结点与结点的父 ...
- A星算法(Java实现)
一.适用场景 在一张地图中.绘制从起点移动到终点的最优路径,地图中会有障碍物.必须绕开障碍物. 二.算法思路 1. 回溯法得到路径 (假设有路径)採用"结点与结点的父节点"的关系从 ...
- JAVA根据A星算法规划起点到终点二维坐标的最短路径
工具类 AStarUtil.java import java.util.*; import java.util.stream.Collectors; /** * A星算法工具类 */ public c ...
随机推荐
- 如何在cowboy应用中指定mnesia数据库路径
创建mnesia数据库的步骤简述: 1)定义脚本: -module(mns). -export([setup/0, clean/0]). -record(user, { id, coin, diamo ...
- Excel: 应用Match/Vlookup比较Excel两列的不同数据
假设Excel中有两列,现在要比较两列数据的不同.
- Asp.net MVC 中使用 Ninject 实现依赖注入
松耦合.针对抽象编程.不针对实现编程是面向对象设计的原则.依赖注入就是,在一个类的内部,不通过创建对象的实例而能够获得实现了某个公开接口的对象引用.所谓的控制反转也是相同的意思.把依赖的创建转移到了使 ...
- 设计模式-策略模式---Strategy(对象行为型)
策略模式 1.概念 分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户. 2.代码实现:(模拟鸭子应用)https://git.oschina.net/ipnunu/Desi ...
- 利用jenkins和docker实现持续交付
利用jenkins和docker实现持续交付 一.什么是持续交付 让软件产品的产出过程在一个短周期内完成,以保证软件可以稳定.持续的保持在随时可以发布的状况.它的目标在于让软件的构建.测试与发布变得更 ...
- 瞎比比系列---1st
A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...
- scikit-learning教程(二)统计学习科学数据处理的教程二
模型选择:选择估计量及其参数 得分和交叉验证的分数 如我们所看到的,每个估计者都会公开一种score可以判断新数据的拟合质量(或预测)的方法.越大越好. >>> >>&g ...
- h5-27-存储/读取JS对象
存储JS对象 <script type="text/javascript"> /*封装人员信息*/ function Person(id,name,age) { thi ...
- 关于itchat用法的一篇博文
itchat的原理就是利用爬虫爬取了网页版微信的内容,并进行一系列的操作,运用微信,通过手机与电脑时登录的互通性,可以实现用微信对电脑的操作,通过itchat.msg_register方法,可以得到目 ...
- (024)[工具软件]截屏录屏软件FSCapture(转)
该软件比 Snipaste 增加的功能有滚动截图和屏幕录制. 原文地址:https://www.appcgn.com/faststone-capture.html FastStoneCapture,简 ...