• 大小: 4KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: 其他
  • 标签: A*算法  最短路经  

资源简介

包含A星算法的核心实现类,封装了简单的地图二维数组,给出开始结束位置以及地图信息,便可以利用该算法寻找一条最短路径

资源截图

代码片段和文件信息

package com.core;
/*
 * A星算法实现类
 * 作者:周学文
 * 邮箱:1414628406@qq.com
 * 
 */
import java.util.ArrayList;
import java.util.List;
public class ASartFindPath {
//开启列表
private static List openList=new ArrayList();
//关闭列表
private static List closeList=new ArrayList();

public ASartFindPath(){

}
/**
 * @param List 返回一个List列表,该列表包含了所找出的路径Location集合
 * @param Location start 开始位置
 * @param Location dest 结束位置
 */
public static List findPath(Location startLocation destAStartMap aStartMap){//起始位置,终点位置,地图
List path = new ArrayList();
openList.add(start);
Location current = null;
do{
current = getLowestFscoreLocation(openList);//current设置为从开放列表了选取F值最小的
closeList.add(current);//将其加入到封闭列表
openList.remove(current);//开放列表里移除当前的方格
if(closeList.contains(dest)){//如果封闭列表包含了终点位置,则循环结束
break;
}
List adjacentLocations = getWalkableAdjacentLocations(current aStartMap);//得到可以走的节点
for(Location lo : adjacentLocations){
if(closeList.contains(lo)){//如果封闭列表里已有此节点,则结束本次循环
continue;
}
if(!openList.contains(lo)){//如果相邻方块不在开放列表中,则将其添加到开放列表中
lo.setMovedSteps(current.getMovedSteps()+1);//g值加1
lo.setEvalRemainSteps(evalRemainSteps(currentdest));//设置H值
lo.setTotalEvalSteps(evalRemainSteps(currentdest)+lo.getMovedSteps());
openList.add(lo);
}else{
if(current.getMovedSteps()+1 < lo.getMovedSteps()){
lo.setMovedSteps(current.getMovedSteps()+1 );
lo.setPrevious(current);
}
}
}

}while(!openList.isEmpty());
//循环遍历封闭列表
Location destination = null;
if(closeList.contains(dest)){
destination = current;

path.add(destination);
while(destination.getPrevious() != null){
destination = destination.getPrevious();
path.add(destination);
}
}

return path;
}

/**
 * 寻找的当前位置附近四个可走的位置
 * @param List 返回一个List列表,该列表包含了所找出四个位置Location集合
 * @param Location current 当前位置
 * @param AStartMapa StartMap A星地图(二维数组)
 */
 
private static List getWalkableAdjacentLocations(Location currentAStartMap aStartMap){
int x = current.getX();//得到当前位置的X值
int y = current.getY();//得到当前位置的y值
List walkableLos = new ArrayList();//新建一个可以走的节点列表
Location location = null;//新建一个临时节点
//map[0].length
int  aStartMap0Length=aStartMap.getMaps()[0].length;
//map.length
int  aStartMapLength=aStartMap.getMaps().length;
//获二维数组
int[][] maps=aStartMap.getMaps();
//获得可以走的节点标志,如1
int accessLocation=aStartMap.getAccessLocation();
if(x+1 < aStartMap0Length && (maps[x+1][y] == accessLocation )){//将右节点是可以走的通路的加入到可走节点列表中
location = new Location(x+1y);
location.setPrevious(current);
walkableLos.add(location);
}
if(x-1>0 && (maps[x-1][y] == accessLocation )){
location = new Location(x-1y);//将左节点是可以走的通路的加入到可走节点列表中
location.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    .......      4951  2014-10-31 02:04  core\ASartFindPath.java

    .......      1603  2014-10-31 02:04  core\AStartMap.java

    .......      1515  2014-10-31 02:04  core\Location.java

    .......      2402  2014-10-31 02:04  core\Maps.java

    .......       682  2014-10-31 02:04  core\Tset.java

     目录          0  2014-10-31 18:06  core

----------- ---------  ---------- -----  ----

                11153                    6


评论

共有 条评论