A*寻找最短路径(JAVA)

    距自己上一次在园子里写东西,已经3个月有余了。今天用JAVA写了一下A*寻路,相信这个园子里的大多数人都是知道A*的,在这个断言的基础上,鄙人就不去过分描述了,废话不多说,直接上代码吧!(PS:我用二维数组模拟了二维地图,其中“0”为走得通的路,“1”为墙或其他过不去的障碍物。)   

代码源文件:1.StarNode.java

                 2.AStar.java

import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;



public class StarNode {
	//相邻点
	private LinkedList<StarNode> neighbors = new LinkedList<StarNode>();
	//自身点位置
	Point location;
	//前一个节点
	StarNode searchParent;
	//当前已花费路径成本
	int  hasCost;
	/**
	 * 构造
	 * @param location
	 */
	StarNode(Point  location){
		this.location=location;
	}
	/**
	 * 获取相邻点(8领域)
	 */
	List<StarNode>  getNeighbors(){
		List  neighbors = new ArrayList<StarNode>();
		int  x  =location.x;
		int  y  =location.y;
		// 左上
		neighbors.add(new StarNode(new Point(x-1,y-1)));
		// 左
		neighbors.add(new StarNode(new Point(x-1,y)));
		// 左下
		neighbors.add(new StarNode(new Point(x-1,y+1)));
		//上
		neighbors.add(new StarNode(new Point(x,y-1)));
		//下
		neighbors.add(new StarNode(new Point(x,y+1)));
		//右上
		neighbors.add(new StarNode(new Point(x+1,y-1)));
		//右
		neighbors.add(new StarNode(new Point(x+1,y)));
		//右下
		neighbors.add(new StarNode(new Point(x+1,y+1)));
		return  neighbors;
	}
	
	
	/**
	 * 判断是否为通路
	 */
	boolean  isRoad(int[][]  map){
		int x=location.x;
		int y=location.y; 
		if(x<0|y<0|x>=map[0].length|y>=map.length){
			return false;
		}else{
			return (map[y][x]==0);
		}
	}  
	
	
	/**
	 * 总的路径成本(当前已花费路径成本+预算成本)
	 */
	int   getCost(int[][]  map,StarNode  goal){
		int budget;
		int x=location.x;
		int y=location.y;
		int xOfgoal=goal.location.x;
		int yOfgoal=goal.location.y;
		int distanceX =Math.abs(xOfgoal-x);
		int distanceY = Math.abs(yOfgoal-y);
		budget=(distanceX>distanceY)?distanceX:distanceY;		
		return  (hasCost+budget);
	}
	
	@Override
	/**
	 * 判断是否为同一个点
	 */
	public  boolean    equals(Object  obj){
		if(!(obj  instanceof  StarNode)){
			return  false;
		}else{
			StarNode   node = (StarNode)obj;
			if(node.location.equals(location)){
				return  true;
			}else{
				return  false;
			}
		}		
	}
   
}

 

 

import java.awt.Point;
import java.util.LinkedList;
import java.util.List;



/**
 * A*寻路
 * 
 * @author Administrator
 * 
 */
public class AStar {

	public static List<StarNode> findPath(int[][]  map,StarNode  start,StarNode  goal){
		//open  list
		List<StarNode> openList = new LinkedList<StarNode>();
		//close list
		List<StarNode> closeList = new LinkedList<StarNode>();
		start.searchParent = null;//起始点没有父节点
		start.hasCost=0;//起始点的开销为0
		//将起始节点加进openList
	    openList.add(start);
	    while(!openList.isEmpty()){
	        StarNode  currentNode =findMinCostNode(openList,map,goal);
	    	if((currentNode).equals(goal)){
	    		return getRoute(currentNode);//返回路径
	    	}else{
	    		//把当前节点移入closeList
	    		closeList.add(currentNode);
	    		openList.remove(currentNode);
	    		for (StarNode n : currentNode.getNeighbors()){
	    			//不在openList中,且不在closeList中,且不是障碍物,则放入openList
	    			if(!openList.contains(n)&&!closeList.contains(n)&&n.isRoad(map)){	    				
	    				openList.add(n);
	    				n.searchParent=currentNode;
	    				n.hasCost=currentNode.hasCost+1;
	    			}
	    		} 	    		
	    	}
	    }		
	    return null;
	}
    
	/**
	 * 找出openList中总成本最低的点
	 */
	private static StarNode  findMinCostNode(List<StarNode>  openList,int[][]  map,StarNode  goal){		
		StarNode minCostNode=null;
		if(openList!=null&&openList.size()>0){
			int size=openList.size();
			minCostNode=openList.get(0);
			for (int i=1;i<size;++i){
				StarNode  node=openList.get(i);
				if(node.getCost(map, goal)<minCostNode.getCost(map, goal)){
					minCostNode=node;
				}
			}
		}
		return minCostNode;		
	}	
	
	
	/**
	 * 获取路径
	 */
	public static List<StarNode> getRoute(StarNode node) {
		LinkedList<StarNode> path = new LinkedList<StarNode>();
		while (node != null) {
			path.addFirst(node);
			node = node.searchParent;
		}
		return path;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] map = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
			       { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
		StarNode   start=new   StarNode(new Point(5,10));
		StarNode   goal=new   StarNode(new Point(6,6));
		List<StarNode> path=findPath(map,start,goal);
		if(path==null){
			System.out.println("此路不通");
		}else{
			for(StarNode node : path){
				System.out.println(node.location.x+","+node.location.y);
			}
		}

	}

}

 

posted on 2013-03-13 19:32  BlueSpirit  阅读(544)  评论(0)    收藏  举报

导航