A Star 最短路径算法的Java实现

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

运行效果如下

 
package  test.star;
import  java.awt.Point;
import  java.util.LinkedList;
/** */ /** */
/** */ /**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:描述路径节点用类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * <p>
 * License: 
http://www.apache.org/licenses/LICENSE-2.0
 * </p>
 * 
 * 
@author chenpeng
 * @email:[email protected]
 * 
@version 0.1
 
*/

public   class  Node  implements  Comparable  ... {
  
// 坐标
  public Point _pos;
  
// 开始地点数值
  public int _costFromStart;
  
// 目标地点数值
  public int _costToObject;
  
// 父节点
  public Node _parentNode;
  
private Node() ...{
  }

  
/** *//** */
  
/** *//**
   * 以注入坐标点方式初始化Node
   * 
   * 
@param _pos
   
*/

  
public Node(Point _pos) ...{
    
this._pos = _pos;
  }

  
/** *//** */
  
/** *//**
   * 返回路径成本
   * 
   * 
@param node
   * 
@return
   
*/

  
public int getCost(Node node) ...{
    
// 获得坐标点间差值 公式:(x1, y1)-(x2, y2)
    int m = node._pos.x - _pos.x;
    
int n = node._pos.y - _pos.y;
    
// 取两节点间欧几理德距离(直线距离)做为估价值,用以获得成本
    return (int) Math.sqrt(m * m + n * n);
  }

  
/** *//** */
  
/** *//**
   * 检查node对象是否和验证对象一致
   
*/

  
public boolean equals(Object node) ...{
    
// 验证坐标为判断依据
    if (_pos.x == ((Node) node)._pos.x && _pos.y == ((Node) node)._pos.y) ...{
      
return true;
    }

    
return false;
  }

  
/** *//** */
  
/** *//**
   * 比较两点以获得最小成本对象
   
*/

  
public int compareTo(Object node) ...{
    
int a1 = _costFromStart + _costToObject;
    
int a2 = ((Node) node)._costFromStart + ((Node) node)._costToObject;
    
if (a1 < a2) ...{
      
return -1;
    }
 else if (a1 == a2) ...{
      
return 0;
    }
 else ...{
      
return 1;
    }

  }

  
/** *//** */
  
/** *//**
   * 获得上下左右各点间移动限制区域
   * 
   * 
@return
   
*/

  
public LinkedList getLimit() ...{
    LinkedList limit 
= new LinkedList();
    
int x = _pos.x;
    
int y = _pos.y;
    
// 上下左右各点间移动区域(对于斜视地图,可以开启注释的偏移部分,此时将评估8个方位)
    
// 上
    limit.add(new Node(new Point(x, y - 1)));
    
// 右上
    
// limit.add(new Node(new Point(x+1, y-1)));
    
// 右
    limit.add(new Node(new Point(x + 1, y)));
    
// 右下
    
// limit.add(new Node(new Point(x+1, y+1)));
    
// 下
    limit.add(new Node(new Point(x, y + 1)));
    
// 左下
    
// limit.add(new Node(new Point(x-1, y+1)));
    
// 左
    limit.add(new Node(new Point(x - 1, y)));
    
// 左上
    
// limit.add(new Node(new Point(x-1, y-1)));
    return limit;
  }

}
 
package  test.star;
import  java.awt.Point;
import  java.util.LinkedList;
import  java.util.List;
/** */ /** */
/** */ /**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:A*寻径处理用类(此类为演示用,并不意味着算法是最佳实现)
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * <p>
 * License: 
http://www.apache.org/licenses/LICENSE-2.0
 * </p>
 * 
 * 
@author chenpeng
 * @email:[email protected]
 * 
@version 0.1
 
*/

public   class  PathFinder  ... {
  
// 路径优先等级list(此示例中为内部方法)
  private LevelList _levelList;
  
// 已完成路径的list
  private LinkedList _closedList;
  
// 地图描述
  private int[][] _map;
  
// 行走区域限制
  private int[] _limit;
  
/** *//** */
  
/** *//**
   * 以注入地图的2维数组及限制点描述初始化此类
   * 
   * 
@param _map
   
*/

  
public PathFinder(int[][] map, int[] limit) ...{
    _map 
= map;
    _limit 
= limit;
    _levelList 
= new LevelList();
    _closedList 
= new LinkedList();
  }

  
/** *//** */
  
/** *//**
   * A*方式寻径,注入开始坐标及目标坐标后运算,返回可行路径的List
   * 
   * 
@param startPos
   * 
@param objectPos
   * 
@return
   
*/

  
public List searchPath(Point startPos, Point objectPos) ...{
    
// 初始化起始节点与目标节点
    Node startNode = new Node(startPos);
    Node objectNode 
= new Node(objectPos);
    
// 设定起始节点参数
    startNode._costFromStart = 0;
    startNode._costToObject 
= startNode.getCost(objectNode);
    startNode._parentNode 
= null;
    
// 加入运算等级序列
    _levelList.add(startNode);
    
// 当运算等级序列中存在数据时,循环处理寻径,直到levelList为空
    while (!_levelList.isEmpty()) ...{
      
// 取出并删除最初的元素
      Node firstNode = (Node) _levelList.removeFirst();
      
// 判定是否和目标node坐标相等
      if (firstNode.equals(objectNode)) ...{
        
// 是的话即可构建出整个行走路线图,运算完毕
        return makePath(firstNode);
      }
 else ...{
        
// 否则
        
// 加入已验证List
        _closedList.add(firstNode);
        
// 获得firstNode的移动区域
        LinkedList _limit = firstNode.getLimit();
        
// 遍历
        for (int i = 0; i < _limit.size(); i++...{
          
// 获得相邻节点
          Node neighborNode = (Node) _limit.get(i);
          
// 获得是否满足等级条件
          boolean isOpen = _levelList.contains(neighborNode);
          
// 获得是否已行走
          boolean isClosed = _closedList.contains(neighborNode);
          
// 判断是否无法通行
          boolean isHit = isHit(neighborNode._pos.x, neighborNode._pos.y);
          
// 当三则判定皆非时
          if (!isOpen && !isClosed && !isHit) ...{
            
// 设定costFromStart
            neighborNode._costFromStart = firstNode._costFromStart + 1;
            
// 设定costToObject
            neighborNode._costToObject = neighborNode.getCost(objectNode);
            
// 改变neighborNode父节点
            neighborNode._parentNode = firstNode;
            
// 加入level
            _levelList.add(neighborNode);
          }

        }

      }

    }

    
// 清空数据
    _levelList.clear();
    _closedList.clear();
    
// 当while无法运行时,将返回null
    return null;
  }

  
/** *//** */
  
/** *//**
   * 判定是否为可通行区域
   * 
   * 
@param x
   * 
@param y
   * 
@return
   
*/

猜你喜欢

转载自blog.csdn.net/jgfyyfd/article/details/84138009