1.[代码][Java]代码
public List findPath(Location start,Location dest,int[][] map){
List path = new ArrayList();
openList.add(start);
Location current = null;
do{
current = getLowestFscoreLocation(openList);
//System.out.println(current.toString());
closeList.add(current);
openList.remove(current);
if(closeList.contains(dest)){
break;
}
List adjacentLocations = getWalkableAdjacentLocations(current, map);
for(Location lo : adjacentLocations){
if(closeList.contains(lo)){
continue;
}
if(!openList.contains(lo)){
lo.setMovedSteps(current.getMovedSteps()+1);
lo.setEvalRemainSteps(evalRemainSteps(current,dest));
lo.setTotalEvalSteps(evalRemainSteps(current,dest)+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;
}