首页 技术 正文
技术 2022年11月16日
0 收藏 421 点赞 4,139 浏览 3368 个字

摘自: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 forif (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 + ")";
}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918