-
大小: 4KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-14
- 語(yǔ)言: 其他
- 標(biāo)簽: A*算法??最短路經(jīng)??
資源簡(jiǎn)介
包含A星算法的核心實(shí)現(xiàn)類,封裝了簡(jiǎn)單的地圖二維數(shù)組,給出開始結(jié)束位置以及地圖信息,便可以利用該算法尋找一條最短路徑

代碼片段和文件信息
package?com.core;
/*
?*?A星算法實(shí)現(xiàn)類
?*?作者:周學(xué)文
?*?郵箱:1414628406@qq.com
?*?
?*/
import?java.util.ArrayList;
import?java.util.List;
public?class?ASartFindPath?{
//開啟列表
private?static?List?openList=new?ArrayList();
//關(guān)閉列表
private?static?List?closeList=new?ArrayList();
public?ASartFindPath(){
}
/**
?*?@param?List?返回一個(gè)List列表,該列表包含了所找出的路徑Location集合
?*?@param?Location?start?開始位置
?*?@param?Location?dest?結(jié)束位置
?*/
public?static?List?findPath(Location?startLocation?destAStartMap?aStartMap){//起始位置,終點(diǎn)位置,地圖
List?path?=?new?ArrayList();
openList.add(start);
Location?current?=?null;
do{
current?=?getLowestFscoreLocation(openList);//current設(shè)置為從開放列表了選取F值最小的
closeList.add(current);//將其加入到封閉列表
openList.remove(current);//開放列表里移除當(dāng)前的方格
if(closeList.contains(dest)){//如果封閉列表包含了終點(diǎn)位置,則循環(huán)結(jié)束
break;
}
List?adjacentLocations?=?getWalkableAdjacentLocations(current?aStartMap);//得到可以走的節(jié)點(diǎn)
for(Location?lo?:?adjacentLocations){
if(closeList.contains(lo)){//如果封閉列表里已有此節(jié)點(diǎn),則結(jié)束本次循環(huán)
continue;
}
if(!openList.contains(lo)){//如果相鄰方塊不在開放列表中,則將其添加到開放列表中
lo.setMovedSteps(current.getMovedSteps()+1);//g值加1
lo.setEvalRemainSteps(evalRemainSteps(currentdest));//設(shè)置H值
lo.setTotalEvalSteps(evalRemainSteps(currentdest)+lo.getMovedSteps());
openList.add(lo);
}else{
if(current.getMovedSteps()+1? lo.setMovedSteps(current.getMovedSteps()+1?);
lo.setPrevious(current);
}
}
}
}while(!openList.isEmpty());
//循環(huán)遍歷封閉列表
Location?destination?=?null;
if(closeList.contains(dest)){
destination?=?current;
path.add(destination);
while(destination.getPrevious()?!=?null){
destination?=?destination.getPrevious();
path.add(destination);
}
}
return?path;
}
/**
?*?尋找的當(dāng)前位置附近四個(gè)可走的位置
?*?@param?List?返回一個(gè)List列表,該列表包含了所找出四個(gè)位置Location集合
?*?@param?Location?current?當(dāng)前位置
?*?@param?AStartMapa?StartMap?A星地圖(二維數(shù)組)
?*/
?
private?static?List?getWalkableAdjacentLocations(Location?currentAStartMap?aStartMap){
int?x?=?current.getX();//得到當(dāng)前位置的X值
int?y?=?current.getY();//得到當(dāng)前位置的y值
List?walkableLos?=?new?ArrayList();//新建一個(gè)可以走的節(jié)點(diǎn)列表
Location?location?=?null;//新建一個(gè)臨時(shí)節(jié)點(diǎn)
//map[0].length
int??aStartMap0Length=aStartMap.getMaps()[0].length;
//map.length
int??aStartMapLength=aStartMap.getMaps().length;
//獲二維數(shù)組
int[][]?maps=aStartMap.getMaps();
//獲得可以走的節(jié)點(diǎn)標(biāo)志,如1
int?accessLocation=aStartMap.getAccessLocation();
if(x+1? 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);//將左節(jié)點(diǎn)是可以走的通路的加入到可走節(jié)點(diǎn)列表中
location.
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
????.......??????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
- 上一篇:教你自制ST_li
nkV2 - 下一篇:2015QQ登陸界面源代碼
評(píng)論
共有 條評(píng)論