算法之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 ...
随机推荐
- SPOJ:String Play (?)
String Play Milo has a string S of length L. Tutu picks a random prefix and Mota picks a random suff ...
- BZOJ_1264_[AHOI2006]基因匹配Match_树状数组
BZOJ_1264_[AHOI2006]基因匹配Match_树状数组 Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种 ...
- Ruby attr_reader , attr_writer, attr_accessor方法
attr_reader方法------读取实例变量 attr_writer方法------改写实例变量 attr_accessor方法-----读写实例变量 class Person attr_rea ...
- 百度之星资格赛 1003 度度熊与邪恶大魔王(二维dp)
分析 挺好的一道题 dp[i][j]表示打败i颗血j防御力的怪兽需要的最少宝石数 然后就好了,复杂度\(O(n+m*1000*10)\) #include <bits/stdc++.h> ...
- Eclipse新建Maven webapp项目错误的解决方法
新建webapp项目时出现如下错误: 解决步骤如下: 1. 右键点击项目,选择Properties,点击Java Build Path,将默认的JRE移除,点击右侧add Library,选择JRE ...
- mybatis使用要点(2019.5.19)
接口入参 只有一个参数,叫啥都没问题 有两个参数以上,需使用@Param,否则名字依次为0.1.2和param1.param2.param3 一般用#,防sql注入:偶尔用$,比如需要动态表名等 接口 ...
- 即时编译(JIT)
即时编译(JIT : just-in-time compilation): 指计算机领域里,即时编译也被成为动态翻译,是一种通过在运行时将字节码翻译为机器码,从而改善字节码编译语言性能的技术 即时编译 ...
- 合作网络(Corporative Network )并查集+路径压缩
#include <iostream> #include <algorithm> #include <string> using namespace std; + ...
- macos php安装扩展sqlsrv连接sqlserver
Install the PHP Drivers for SQL Serve sudo pecl install pdo_sqlsrv sudo pecl install sqlsrv 微软官方文档 ...
- python堆排序实现TOPK问题
# 构建小顶堆跳转def sift(li, low, higt): tmp = li[low] i = low j = 2 * i + 1 while j <= higt: # 情况2:i已经是 ...