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. Ubuntu和win10双系统Grup无法引导解决方案

    通常我们经常安装双系统, 但是有时候安装完系统无法正常引导, 以下就说明Ubuntu和win10双系统, win10在grub界面不断循环的解决方案 直接在win10启动项目上按e进入编辑模式 在文档 ...

  2. 前台html与后台php通信(上传文件)

    这部分为导入txt文本文件,存放在服务器然后返回txt文本的内容到前台进行相应操作 前台html代码 <div id="coordinate_div">         ...

  3. tcpdump的表达元

    (nt: True 在以下的描述中含义为: 相应条件表达式中只含有以下所列的一个特定表达元, 此时表达式为真, 即条件得到满足) dst host host如果IPv4/v6 数据包的目的域是host ...

  4. [即时通讯]openfire启动失败解决办法

    当你发现你的电脑上的openfire无论你是重新安装还是重启都无法启动的情况下你可以按照我下面写的那些终端指令来启动你的openfire不用再去重新做你的系统了. 一般你发现你的openfire打开出 ...

  5. iOS探究UITableView的内部代码,仿UITableView自定义

    大家都知道UITableView,最经典在于循环利用,这里我自己模仿UITableView循环利用,写了一套自己的TableView实现方案,希望大家看了我的文章,循环利用思想有显著提升. 研究UIT ...

  6. ZOJ 3927 Programming Ability Test

    水题,判断一下加起来是否大于等于80 #include<cstdio> #include<cstring> #include<cmath> #include< ...

  7. CSU 1515 Sequence

    莫队算法+map #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  8. java网络通信之非阻塞通信

    java中提供的非阻塞类主要包含在java.nio,包括: 1.ServerSocketChannel:ServerSocket替代类,支持阻塞与非阻塞: 2.SocketChannel:Socket ...

  9. 在阿里云ECS(CentOS6.5)上安装jdk

    JDK安装 在安装前先确定服务器上没有安装过JDK 命令: java -version 结果: 查看所有java安装包 命令: yum -y list java* 结果: 选择安装所需要的JDK 命令 ...

  10. Redis详细介绍

    转自:http://blog.csdn.net/eroswang/article/details/7080412 1.介绍 1.1 Redis是什么 REmote DIctionary Server( ...