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. 转:zip版Tomcat(6,7)不能注册成windows服务或者注册后不能启动的问题解决

    源地址:http://www.iflym.com/index.php/code/zip-tomcat67-can-not-register-windows-services-resolve.html ...

  2. MD5Util1

    package com.cc.hkjc.util; import java.math.BigInteger;import java.security.MessageDigest;import java ...

  3. 无限轮播器的bug修复

    前言:上一回实现了轮播器的自动滚动,但是有两个需要处理的bug. 1.增加需求:当用手拖拽控制轮播器的时候,停止自动滚动. 2.当同一个页面中有tableView,textView或scrollvie ...

  4. Bootstrap-CL:按钮组

    ylbtech-Bootstrap-CL:按钮组 1.返回顶部 1. Bootstrap 按钮组 按钮组允许多个按钮被堆叠在同一行上.当你想要把按钮对齐在一起时,这就显得非常有用.您可以通过 Boot ...

  5. bzoj2839

    容斥原理+组合数学 看见这种恰有k个的题一般都是容斥原理,因为恰有的限制比较强,一般需要复杂度较高的方法枚举,而容斥就是转化为至少有k个,然后通过容斥原理解决 我们先选出k个元素作为交集,有C(n,k ...

  6. C++实现利用(前序和中序生成二叉树)以及(二叉树的镜像)

    #include<iostream> #include<string.h> #include<stack> using namespace std; typedef ...

  7. Unity3D将来时:IL2CPP(上)

    http://inpla.net/thread-8197-1-1.html Unity3D将来时:IL2CPP(上) (注:本文详细的讲述了C#,Mono,.Net, IL等Unity使用到的概念,如 ...

  8. Cocos2d-html5游戏开发,常用工具集合

    代码编辑器IDEWebStorm (Windows, Mac) Cocos2d-html5官方团队在用,非常优秀的工具,请大家支持正版动画编辑器 Animation EditorSpriteHelpe ...

  9. jzoj5980. 【WC2019模拟12.27】字符串游戏

    首先发现双方可以有一个默契,不管谁刻意,都可以把串变为诸如\(...101010101...\)的形式 所以先手要赢的话就是要在上面的基础之上加一个字符使其变为要求的子串 那么就是要求的子串中相邻两个 ...

  10. P2579 [ZJOI2005]沼泽鳄鱼

    传送门 话说邻接矩阵居然还能快速幂的么-- 把原图的邻接矩阵\(G\)打出来,那么\(G[u][v]\)表示一秒后\(u\)到\(v\)的方案数,\(G^2[u][v]\)表示\(2\)秒后的方案数- ...