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星算法(寻路)的更多相关文章

  1. A*搜寻算法(A星算法)

    A*搜寻算法[编辑] 维基百科,自由的百科全书 本条目需要补充更多来源.(2015年6月30日) 请协助添加多方面可靠来源以改善这篇条目,无法查证的内容可能会被提出异议而移除. A*搜索算法,俗称A星 ...

  2. 算法 A-Star(A星)寻路

    一.简介 在游戏中,有一个很常见地需求,就是要让一个角色从A点走向B点,我们期望是让角色走最少的路.嗯,大家可能会说,直线就是最短的.没错,但大多数时候,A到B中间都会出现一些角色无法穿越的东西,比如 ...

  3. JS算法之A*(A星)寻路算法

    今天写一个连连看的游戏的时候,接触到了一些寻路算法,我就大概讲讲其中的A*算法. 这个是我学习后的一点个人理解,有错误欢迎各位看官指正. 寻路模式主要有三种:广度游戏搜索.深度优先搜索和启发式搜索. ...

  4. 算法起步之A星算法

    原文:算法起步之A星算法 用途: 寻找最短路径,优于bfs跟dfs 描述: 基本描述是,在深度优先搜索的基础上,增加了一个启发式算法,在选择节点的过程中,不是盲目选择,而是有目的的选的,F=G+H,f ...

  5. Cocos2d-x 3.1.1 学习日志16--A星算法(A*搜索算法)学问

    A *搜索算法称为A星算法.这是一个在图形平面,路径.求出最低通过成本的算法. 经常使用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上. 首先:1.在Map地图中任取2个点,開始点和结束点 ...

  6. POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang

    题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...

  7. Java开源-astar:A 星算法

    astar A星算法Java实现 一.适用场景 在一张地图中,绘制从起点移动到终点的最优路径,地图中会有障碍物,必须绕开障碍物. 二.算法思路 1. 回溯法得到路径 (如果有路径)采用“结点与结点的父 ...

  8. A星算法(Java实现)

    一.适用场景 在一张地图中.绘制从起点移动到终点的最优路径,地图中会有障碍物.必须绕开障碍物. 二.算法思路 1. 回溯法得到路径 (假设有路径)採用"结点与结点的父节点"的关系从 ...

  9. JAVA根据A星算法规划起点到终点二维坐标的最短路径

    工具类 AStarUtil.java import java.util.*; import java.util.stream.Collectors; /** * A星算法工具类 */ public c ...

随机推荐

  1. POJ - 2417 Discrete Logging(Baby-Step Giant-Step)

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  2. js 购物车中,多件商品数量加减效果修改,实现总价随数量加减改变

    <!DOCTYPE html> <html> <head> <meta charset=UTF-8 /> <title>无标题文档</ ...

  3. Windows 上 GitHub Desktop 的操作

    目 录 第1章 上传开源代码至GitHub    1 1.1 git Windows 客户端    1 1.2 注册GitHub账户    2 1.3 登录    2 1.4 创建本地代码仓库     ...

  4. 分区时"磁盘上没有足够的空间完成此操作"的解决方法

    在新的预装windows 7的品牌机上,工作人员一般将磁盘分为C.D两个分区.但往往造成C盘有很大一部分的空间没办法分出来,而分出来的部分空间又不能和后面的磁盘合并,甚至出现无法新建简单卷的操作,即点 ...

  5. OpenCV视频的读写

    实验室的项目是处理视频,所以就从视频的读取和写入开始吧! 常用的接口有C++和Python,Python肯定要简洁许多,不过因为项目需要,还是用C++了(PS:其实是我被Python的速度惊到了... ...

  6. Collection View Programming Guide for iOS---(一)----About iOS Collection Views

    Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...

  7. kubernetes1.13.1部署ingress-nginx-十一

    一.Ingress 简介 (1) 在Kubernetes中,服务和Pod的IP地址仅可以在集群网络内部使用,对于集群外的应用是不可见的. 为了使外部的应用能够访问集群内的服务, 在Kubernetes ...

  8. 解决Excel打开UTF-8编码CSV文件乱码的问题

    打开 Excel,执行“数据”->“自文本”,选择 CSV 文件,出现文本导入向导,选择“分隔符号”,下一步,勾选“逗号”,去掉“ Tab 键”,下一步,完成,在“导入数据”对话框里,直接点确定 ...

  9. PHP empty()函数使用需要注意

    在 PHP 5.5 之前,empty() 仅支持变量:任何其他东西将会导致一个解析错误.换言之,下列代码不会生效: empty(trim($name)). 作为替代,应该使用trim($name) = ...

  10. poj 3648 Wedding【2-SAT+tarjan+拓扑】

    看错题*n,注意是输出新娘这边的-- 按2-SAT规则连互斥的边,然后注意连一条(1,1+n)表示新娘必选 然后输出color[belong[i]]==color[belong[1+n(新娘)]]的点 ...