import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Queue; public class TestPath {
class Point implements Comparable<Point> {
int x; // 坐标x
int y; // 坐标y
int f = 200; // 估值数 f = g + h
int g = 100;  // 与父节点的距离
int h = 100;  // 与目标点的距离
boolean canWalk;  // 是否可以通行
Point parent = null; Point(int x, int y, boolean canWalk) {
this.x = x;
this.y = y;
this.canWalk = canWalk;
} @Override
public int compareTo(Point o) {
return new Integer(f).compareTo(new Integer(o.f));
} } public static void main(String[] args) {
int arr[][] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
Point[][] paths = new Point[10][10];
       // 生成路径对象数组
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
boolean canWalk = arr[i][j] == 1 ? false : true;
paths[i][j] = new TestPath().new Point(i, j, canWalk);
}
}
      // 起始点
Point bp = paths[0][0];
      // 目标点
Point ep = paths[2][4];
AStart(paths, bp, ep);
Point tmp = ep;
while (tmp.parent != bp) {
tmp = tmp.parent;
System.out.println(tmp.x + "," + tmp.y);
} } static void AStart(Point[][] paths, Point beginP, Point endP) {
       // 待遍历列表
ArrayList<Point> openList = new ArrayList<>(100);
beginP.g = 0;
beginP.h = (Math.abs(beginP.x - endP.x) + Math.abs(beginP.y - endP.y)) * 10;
beginP.f = beginP.g + beginP.h;
endP.h = 0;
openList.add(beginP);
ArrayList<Point> closeList = new ArrayList<>(100);
//Iterator<Point> iter = openList.iterator();
while (openList.size() != 0) {
Point p = openList.get(0);
System.out.println("p: x="+p.x + ",y="+p.y + ",g="+p.g + ",h="+ p.h +",f="+p.f);
if (p.h == 0) {
System.out.println("=========end===========");
System.out.println(p.x + "," + p.y);
return;
}
          // 遍历周边相邻路径
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (p.x + i < 0 || p.x + i > 9 || p.y + j < 0 || p.y + j > 9) { // 超出地图范围
continue;
}
if (i == 0 && j == 0) { // 本身
continue;
}
Point tmp = paths[p.x + i][p.y + j];
              // 如果已经遍历过了,跳过
if (!tmp.canWalk || closeList.contains(tmp)) {
continue;
}
              // 下一个路径点的g值,相邻的加10,对角的加14(10和14是自己定义的值)
int newG = 0;
if ( i != 0 && j != 0) {
newG = p.g + 14;
} else {
newG = p.g + 10;
}
System.out.println("newg="+newG);
              // 下一个路径点与目标点的h值
tmp.h = (Math.abs(tmp.x - endP.x) + Math.abs(tmp.y - endP.y)) * 10;
if (openList.contains(tmp)) {
if (tmp.g > newG) { // 在待遍历列表中,并且与当前的路径点距离更近,则更换上级路径点
tmp.parent = p;
tmp.g = newG;
tmp.f = tmp.g + tmp.h;
}
} else {// 加入待遍历列表
tmp.parent = p;
tmp.g = newG;
openList.add(tmp);
}
tmp.f = tmp.g + tmp.h; // 更新f值
System.out.println(tmp.x + "," + tmp.y + "," + tmp.g+","+tmp.h+","+tmp.f+",("+tmp.parent.x + ","+ tmp.parent.y+")");
}
}
          // 遍历完之后从待遍历列表中移除,加入到已遍历列表中
closeList.add(p);
openList.remove(p);
          // 按f值排序
Collections.sort(openList);
//iter = openList.iterator();
}
} }

JAVA版A星算法实现的更多相关文章

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

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

  2. java版数据结构与算法第二章数组

    数组由一组具有相同类型的数据元素组成,并存储在一组连续存储单元中.一维数组是常量. 二维数组:若一维数组中的数据元素又是一堆数据结构,我们称之为二维数组.二维数组可以看成是n个列向量组成的线性表. 数 ...

  3. java版数据结构与算法 (1综述)

    很大部分转载自 https://blog.csdn.net/singit/article/details/54898316 数据的逻辑结构:反映数据元素之间的逻辑关系的数据结构,其中的逻辑关系指数据元 ...

  4. Java版各种排序算法 (冒泡,快速,选择,插入)

    package com.test4; import java.util.*; //Calendar 显示时间 /** * @author qingfeng * 功能:排序算法 */ public cl ...

  5. 数据结构Java版之交换算法(一)

    交换的本质是拷贝,其中拷贝包括两种方式.值拷贝和指针拷贝,在java中没有指针,为此,我们可以理解为地址拷贝,在我看来,指针就是地址. 1.传值方式示例: 由上述示例可得,传值,不能起到交换的作用,原 ...

  6. 数据结构Java版之查找算法(三)

    关于查找算法,这里只进行两个算法的说明.包括 顺序查找 和 折半查找. 顺序查找: 顺序查找常用于未排序的数据中.查找速度较慢,只能应用于较小的数据量. public int sequentialSe ...

  7. 数据结构Java版之排序算法(二)

    排序按时间复杂度和空间复杂度可分为 低级排序 和 高级排序 算法两种.下面将对排序算法进行讲解,以及样例的展示. 低级排序:冒泡排序.选择排序.插入排序. 冒泡排序: 核心思想,小的数往前移.假设最小 ...

  8. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  9. 剑指Offer——回溯算法解迷宫问题(java版)

    剑指Offer--回溯算法解迷宫问题(java版)   以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路.   下面我们来详细讲一 ...

随机推荐

  1. iOS面向对象的建模:MVC(OC基础)

    本文转发至:http://www.cnblogs.com/tmf-4838/p/5294036.html 实例化一个类:从plist文件抽取出类 @interface Person : NSObjec ...

  2. 缩进(Python很将就格式)

    空白在Python中是重要的.事实上行首的空白是重要的.它称为缩进.在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组.这意味着同一层次的语句必须有相同的缩进.每一组这样 ...

  3. (简单) POJ 3268 Silver Cow Party,Dijkstra。

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  4. Arduino线程库ProtoThreads

    参考: Arduino线程库ProtoThreads 一个“蝇量级” C 语言协程库

  5. 乘方快速幂 OR 乘法快速幂

    关于快速幂这个算法,已经不想多说,很早也就会了这个算法,但是原来一直靠着模板云里雾里的,最近重新学习,发现忽视了一个重要的问题,就是若取模的数大于int型,即若为__int64的时候应该怎么办,这样就 ...

  6. linux命令学习-3-sysctl

    sysctl 内核变量配置 Usage: sysctl [options] [variable[=value] ...]   NAME sysctl - configure kernel parame ...

  7. XCode里的模拟器到底在哪里?我的App被放到哪里了?如何寻找真机的沙盒文件?

    一. 开发iOS,必然少不了和XCode这个家伙打交道.平时我们调试自己的App的时候,最常用到的就是模拟器Simulator了,调试的时候,我们的App会自动被XCode安装到模拟器中去,不过: 你 ...

  8. ubuntu12.04+fuerte 下跑通lsd-slam——使用usb摄像头

    上一篇介绍了如何使用数据集跑lsd-slam,这篇介绍如何用一个普通的usb摄像头跑lsd-slam,默认ubuntu12.04,fuerte已经安装好,workspace也已设置,如果没有,请参考上 ...

  9. Led控件

    在 WindowMobile 上的模拟LED 显示屏插件 一个简单Led控件 一个经典的控制Led的单片机程序 Led控件(2)——Led显示屏模拟

  10. 在GitHub上创建上传下载开源项目代码

    1.注册GitHub帐号,创建GitHub项目代码仓库 1.1.注册GitHub帐号 在使GitHub之前,需要先登录其官网注册一个免费使用的账号.登录 https://github.com/join ...