資源簡介
包含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.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? 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
- 上一篇:教你自制ST_li
nkV2 - 下一篇:2015QQ登陸界面源代碼
評論
共有 條評論