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. CodeForces 618C CodeForces 618C

    第一反应是在凸包上随便找一条边,然后找剩下n-2个点里面距离这条边最短的一个点,这三点就构成了符合要求的三角形..然而..精度被卡死. 换种思路,随便找两个点P1,P2,找剩下n-2个点中哪一个点与P ...

  2. Xcode崩溃问题调试 signal SIGABRT&EXC_BAD_ACCESS

    在进行app开发过程中会遇到很多的问题,各种崩溃令人相当头疼.当然,解决bug的能力也体现了一个程序员的水平,现在来说一说开发中经常遇到的崩溃问题吧. 常见崩溃问题: 一是signal SIGABRT ...

  3. ie6,ie7兼容性总结

    摘自: http://www.cnblogs.com/li0803/archive/2009/08/22/1552094.html 其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致 ...

  4. 【转】Linux强大命令 Awk 20分钟入门介绍

    什么是Awk Awk是一种小巧的编程语言及命令行工具.(其名称得自于它的创始人Alfred Aho.Peter Weinberger 和 Brian Kernighan姓氏的首个字母).它非常适合服务 ...

  5. struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)

    一.web.xml配置 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...

  6. 【NOIP2015】反思+题解

    D1T1> 神奇的幻方 模拟即可. #include <cstdio> #include <cstring> #include <algorithm> #de ...

  7. RabbitMQ消息队列(三):任务分发机制

    在上篇文章中,我们解决了从发送端(Producer)向接收端(Consumer)发送“Hello World”的问题.在实际的应用场景中,这是远远不够的.从本篇文章开始,我们将结合更加实际的应用场景来 ...

  8. osgEarth基础入门(转载)

    osgEarth基础入门 osgEarth是基于三维引擎osg开发的三维数字地球引擎库,在osg基础上实现了瓦片调度插件,可选的四叉树调度插件,更多的地理数据加载插件(包括GDAL,ogr,WMS,T ...

  9. MongoDB的$type操作符

    字段类型定义: db.col.find({"title" : {$type : 2}})

  10. apue chapter 4 文件和目录

    1.文件信息结构体 struct stat{ mode_t st_mode; //file type and permissions ino_t st_ino; //i-node number (se ...