【使用Java编写的B*算法】

使用Java编写的B*算法

  package rpg.stage.path;

  import java.util.ArrayList;

  import java.util.HashSet;

  import java.util.Iterator;

  import rpg.objs.Point;

  public class BFinding {

  public BFinding() {

  }

  protected HashSet<Point> openList = new HashSet<Point>();

  protected HashSet<Point> leftList = new HashSet<Point>();

  protected HashSet<Point> rightList = new HashSet<Point>();

  protected HashSet<Point> closeList = new HashSet<Point>();

  public synchronized ArrayList<int[]> find(Point start,Point end,boolean canPenetrate){

  if(end == null){

  return new ArrayList<int[]>();

  }

  if(start == null){

  return new ArrayList<int[]>();

  }

  end.clear();

  start.clear();

  openList.clear();

  openList.add(start);

  leftList.clear();

  rightList.clear();

  closeList.clear();

  int count = 0;

  while(!openList.isEmpty() || !leftList.isEmpty() || !rightList.isEmpty()){

  count ++;

  if(count>1000)

  break;

  Iterator<Point> it = openList.iterator();

  if(it.hasNext()){

  Point p = it.next();

  it.remove();

  if(sideNext(p,end,0,canPenetrate))break;

  }

  it = leftList.iterator();

  if(it.hasNext()){

  Point p = it.next();

  it.remove();

  if(sideNext(p,end,1,canPenetrate))break;

  }

  it = rightList.iterator();

  if(it.hasNext()){

  Point p = it.next();

  it.remove();

  if(sideNext(p,end,-1,canPenetrate))break;

  }

  }

  final ArrayList<int[]> list = new ArrayList<int[]>();

  while(end.parent!=null){

  list.add(0,new int[]{end.x,end.y});

  end = end.parent;

  }

  return list;

  }

  /**

  *

  * @param p

  * @param end 目标点

  * @param side 0 direct -1 right 1 left

  * @param canPenetrate 可否穿透

  */更多精彩教程请关注:深度纯净雨林木风安装版

posted @ 2013-08-06 09:30  豆豆逗逗  阅读(299)  评论(0编辑  收藏  举报