摘自:http://www.cnblogs.com/hxsyl/p/3994730.html

A*算法的java实现

import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack; /**
* @author pang
*
*/
public class AStartPathFind { //前四个上下左右
public final static int[] dx = { 0, -1, 0, 1, -1, -1, 1, 1 };
public final static int[] dy = { -1, 0, 1, 0, 1, -1, -1, 1 }; // 最外圈都是1表示不可通过
final static public 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 } }; public static void main(String[] args) {
// TODO Auto-generated method stub
Point start = new Point(1, 1);
Point end = new Point(10, 13);
/*
* 第一个问题:起点FGH需要初始化吗? 看参考资料的图片发现不需要
*/
Stack<Point> stack = printPath(start, end);
if (null == stack) {
System.out.println("不可达");
} else {
while (!stack.isEmpty()) {
// 输出(1,2)这样的形势需要重写toString
System.out.print(stack.pop() + " -> ");
}
System.out.println();
} } public static Stack<Point> printPath(Point start, Point end) { /*
* 不用PriorityQueue是因为必须取出存在的元素
*/
ArrayList<Point> openTable = new ArrayList<Point>();
ArrayList<Point> closeTable = new ArrayList<Point>();
openTable.clear();
closeTable.clear();
Stack<Point> pathStack = new Stack<Point>();
start.parent = null;
// 该点起到转换作用,就是当前扩展点
Point currentPoint = new Point(start.x, start.y);
// closeTable.add(currentPoint);
boolean flag = true; while (flag) {
for (int i = 0; i < 8; i++) {
int fx = currentPoint.x + dx[i];
int fy = currentPoint.y + dy[i];
Point tempPoint = new Point(fx, fy);
if (map[fx][fy] == 1) {
// 由于边界都是1中间障碍物也是1,,这样不必考虑越界和障碍点扩展问题
// 如果不设置边界那么fx >=map.length &&fy>=map[0].length判断越界问题
continue;
} else {
if (end.equals(tempPoint)) {
flag = false;
// 不是tempPoint,他俩都一样了此时
end.parent = currentPoint;
break;
}
if (i < 4) {
tempPoint.G = currentPoint.G + 10;
} else {
tempPoint.G = currentPoint.G + 14;
}
tempPoint.H = Point.getDis(tempPoint, end);
tempPoint.F = tempPoint.G + tempPoint.H;
// 因为重写了equals方法,所以这里包含只是按equals相等包含
// 这一点是使用java封装好类的关键
if (openTable.contains(tempPoint)) {
int pos = openTable.indexOf(tempPoint);
Point temp = openTable.get(pos);
if (temp.F > tempPoint.F) {
openTable.remove(pos);
openTable.add(tempPoint);
tempPoint.parent = currentPoint;
}
} else if (closeTable.contains(tempPoint)) {
int pos = closeTable.indexOf(tempPoint);
Point temp = closeTable.get(pos);
if (temp.F > tempPoint.F) {
closeTable.remove(pos);
openTable.add(tempPoint);
tempPoint.parent = currentPoint;
}
} else {
openTable.add(tempPoint);
tempPoint.parent = currentPoint;
} }
}// end for if (openTable.isEmpty()) {
return null;
}// 无路径
if (false == flag) {
break;
}// 找到路径
openTable.remove(currentPoint);
closeTable.add(currentPoint);
Collections.sort(openTable);
currentPoint = openTable.get(0); }// end while
Point node = end;
while (node.parent != null) {
pathStack.push(node);
node = node.parent;
}
return pathStack;
}
} class Point implements Comparable<Point> {
int x;
int y;
Point parent;
int F, G, H; public Point(int x, int y) {
super();
this.x = x;
this.y = y;
this.F = 0;
this.G = 0;
this.H = 0;
} @Override
public int compareTo(Point o) {
// TODO Auto-generated method stub
return this.F - o.F;
} @Override
public boolean equals(Object obj) {
Point point = (Point) obj;
if (point.x == this.x && point.y == this.y)
return true;
return false;
} public static int getDis(Point p1, Point p2) {
int dis = Math.abs(p1.x - p2.x) * 10 + Math.abs(p1.y - p2.y) * 10;
return dis;
} @Override
public String toString() {
return "(" + this.x + "," + this.y + ")";
} }

  

AStar算法的学习的更多相关文章

  1. 推荐一个算法编程学习中文社区-51NOD【算法分级,支持多语言,可在线编译】

    最近偶尔发现一个算法编程学习的论坛,刚开始有点好奇,也只是注册了一下.最近有时间好好研究了一下,的确非常赞,所以推荐给大家.功能和介绍看下面介绍吧.首页的标题很给劲,很纯粹的Coding社区....虽 ...

  2. 关于统计变换(CT/MCT/RMCT)算法的学习和实现

    原文地址http://blog.sina.com.cn/s/blog_684c8d630100turx.html 刚开会每周的例会,最讨厌开会了,不过为了能顺利毕业,只能忍了.闲话不多说了,下面把上周 ...

  3. [原创]南水之源A*(A-Star)算法

    开发导航之前我看了一些A*(A-Star)算法的例子和讲解.没有求得甚解!不过也从A*(A-Star)算法中得到启发,写了一套自己的A*(A-Star)算法.当然,这不是真正(我也不知道)的A*(A- ...

  4. 算法导论学习---红黑树具体解释之插入(C语言实现)

    前面我们学习二叉搜索树的时候发如今一些情况下其高度不是非常均匀,甚至有时候会退化成一条长链,所以我们引用一些"平衡"的二叉搜索树.红黑树就是一种"平衡"的二叉搜 ...

  5. AStar算法()

    把网上的AStar算法的论述自己实现了一遍,一开始只是最基础的实现.当然,现在AStar算法已经演变出了各种优化的版本,这篇也会基于各种优化不断的更新. 如果对算法不熟悉可以看下Stanford的这篇 ...

  6. 启发式搜索A-Star算法 【寻找 最短路径 算法】【地理几何位置 可利用的情况】

    在处理最短路径问题时,有一种启发式算法是我们应该了解的,由于其有着优秀的探索效率在各自现实项目中多有应用,它就是 A-star 算法,或  A*  算法. 个人观点: A*  算法并不保证找到的路径一 ...

  7. 【树论 1】 prim算法的学习和使用

    进阶版神犇可以看看本题解的姊妹篇 Kruskal算法的学习和使用 下面的内容是prim算法 但是最小生成树是什么呢? 标准定义如下:在边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值 ...

  8. 毕业设计预习:SM3密码杂凑算法基础学习

    SM3密码杂凑算法基础学习 术语与定义 1 比特串bit string 由0和1组成的二进制数字序列. 2 大端big-endian 数据在内存中的一种表示格式,规定左边为高有效位,右边为低有效位.数 ...

  9. 第八模块:算法&设计模式、企业应用 第1章 常用算法&设计模式学习

    第八模块:算法&设计模式.企业应用 第1章 常用算法&设计模式学习

随机推荐

  1. [Tool] 取得APP的Store URL Scheme (Android、iOS)

    [Tool] 取得APP的Store URL Scheme (Android.iOS) 前言 在企业网站中,如果希望使用URL连结的方式,开启Store APP来下载APP(非网页下载).开发人员可以 ...

  2. 点击div全选中再点击取消全选div里面的文字

    想做一个就是点击一个div然后实现的功能是div里面的文字都成选中状态,然后就可以利用浏览器的自带的复制功能,任意复制在哪里去了 在网上百度了一下 然后网上的答案感觉很大的范围 然后一些搜索 然后就锁 ...

  3. 当jquery ajax遇上401请求

    jquery ajax是个很常用接口,而在请求时候,可能存在响应401的情况(身份认证过期或未登录),比较容易出现在混合应用上,如何进行身份认证,重发失败请求,还是值得注意的. ajax请求有两种方式 ...

  4. BT5 & Kali Linux 网卡选择

    [需要注意的几点]: 芯片类型 知否支持外接天线 网卡固件版本 支持的无线协议 网卡功率 BT5/Kali  RC3支持网卡: RTL8187, R8187 (Realtek) 功率高.兼容性好 Ra ...

  5. Android Handler机制(二)---MessageQueue源码解析

    MessageQueue 1.变量 private final boolean mQuitAllowed;//表示MessageQueue是否允许退出 @SuppressWarnings(" ...

  6. 转 用C API 操作MySQL数据库

    用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...

  7. Service和Thread的关系及如何启用Service,如何停用Service

    Service和Thread的关系: 不少Android初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread?答案可能会 ...

  8. 【代码笔记】iOS-看图听故事

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFo ...

  9. 【代码笔记】iOS-浮动的云

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  10. socket编程中服务器端常用函数 以及简单实现

    1 常用函数 1.1   socket() int socket(int family, int type, int protocol); socket()打开一个网络通讯端口,如果成功的话,返回一个 ...