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 可否穿透
*/
protected boolean sideNext(Point p,Point end,int side,boolean canPenetrate){
int dir = Point.getDirSimple(p, end);
Point nextp = null; if(closeList.contains(p)){
nextp = nextPassPointSide(p,end,-1,canPenetrate);
if(nextp != null){
if(nextp == end){
nextp.parent = p;
return true;
}
if(this.closeList.contains(nextp))
// return sideNext(nextp, end, side, canPenetrate);
return false;
else if(!this.leftList.contains(nextp))
addToSearch(p,nextp,this.rightList);
}
nextp = nextPassPointSide(p,end,1,canPenetrate);
if(nextp != null){
if(nextp == end){
nextp.parent = p;
return true;
}
if(this.closeList.contains(nextp))
// return sideNext(nextp, end, side, canPenetrate);
return false;
else if(!this.rightList.contains(nextp))
addToSearch(p,nextp,this.leftList);
}
return false;
}
this.closeList.add(p);
if(side == 0){
if(p.canWalkDir(dir,canPenetrate)){//下一个点可以走
nextp = p.getPassPointByDir(dir);
if(nextp == end){
nextp.parent = p;
return true;
}
if(!this.closeList.contains(nextp)){
addToSearch(p,nextp,this.openList);
}
}
else//不可走,就分支出两个围绕探索点
{
nextp = nextPassPointSide(p,end,-1,canPenetrate);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null){
if(this.closeList.contains(nextp))
return sideNext(nextp, end, side, canPenetrate);
// return false;
else if(!this.leftList.contains(nextp))
addToSearch(p,nextp,this.rightList);
}
nextp = nextPassPointSide(p,end,1,canPenetrate);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null){
if(this.closeList.contains(nextp))
return sideNext(nextp, end, side, canPenetrate);
// return false;
else if(!this.rightList.contains(nextp))
addToSearch(p,nextp,this.leftList);
}
}
}
else if(side>0){
nextp = p.getPassPointByDir(dir);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null && !this.closeList.contains(nextp)){
addToSearch(p,nextp,this.openList);
}
else
{
nextp = nextPassPointSide(p,end,1,canPenetrate);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null && !this.closeList.contains(nextp) && !this.rightList.contains(nextp)){
addToSearch(p,nextp,this.leftList);
}
}
}
else if(side<0){
nextp = p.getPassPointByDir(dir);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null && !this.closeList.contains(nextp)){
addToSearch(p,nextp,this.openList);
}
else
{
nextp = nextPassPointSide(p,end,-1,canPenetrate);
if(nextp == end){
nextp.parent = p;
return true;
}
if(nextp != null && !this.closeList.contains(nextp) && !this.leftList.contains(nextp)){
addToSearch(p,nextp,this.rightList);
}
}
}
return false;
} protected void addToSearch(Point parent,Point next,HashSet<Point> list){
next.clear();
next.parent = parent;
list.add(next);
} /**
*
* @param p
* @param side >0 或者 <0
* @param canPenetrate
* @return
*/
protected Point nextPassPointSide(Point p,Point end,int side,boolean canPenetrate){
int dir = Point.getDirSimple(p, end);
Point nextp = null;
if(side<0){
while(side>=-7){
dir = Point.rightdir(dir);
if(p.canWalkDir(dir,canPenetrate)){
nextp = p.getPassPointByDir(dir);
if(!this.closeList.contains(nextp)){
break;
}
}
side--;
}
}
else
{
while(side<=7){
dir = Point.leftdir(dir);
if(p.canWalkDir(dir,canPenetrate)){
nextp = p.getPassPointByDir(dir);
if(!this.closeList.contains(nextp)){
break;
}
}
side++;
}
}
return nextp;
}
}

使用Java编写的B*算法的更多相关文章

  1. 「福利」Java Swing 编写的可视化算法工程,包含树、图和排序

    之前在整理<学习排序算法,结合这个方法太容易理解了>这篇文章时,发现了一个用 Java Swing 编写的可视化算法工程,真心不错!包含了常用数据结构和算法的动态演示,先来张图感受下: 可 ...

  2. 小学四则运算练习(JAVA编写)

    源码在Github的仓库主页链接地址:https://github.com/rucr9/rucr 看到这个题目,大概很多人会发出“切,这也太简单了吧!有必要小题大做?”的感叹!是的,仅仅作为一道数学运 ...

  3. 数据结构与算法【Java】05---排序算法总结

    前言 数据 data 结构(structure)是一门 研究组织数据方式的学科,有了编程语言也就有了数据结构.学好数据结构才可以编写出更加漂亮,更加有效率的代码. 要学习好数据结构就要多多考虑如何将生 ...

  4. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  5. Java中的查找算法之顺序查找(Sequential Search)

    Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...

  6. Java中的经典算法之选择排序(SelectionSort)

    Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...

  7. 网页动物园2.0发布,经过几个月的努力,采用JAVA编写!

    网页动物园2.0发布,经过几个月的努力,采用JAVA编写! 网页动物园2.0 正式发布!游戏发布 游戏名称: 网页动物园插件 游戏来源: 原创插件 适用版本: Discuz! X1.5 - X3.5 ...

  8. Java中的排序算法(2)

    Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...

  9. 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小

    原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...

随机推荐

  1. Hibernate的一些相关信息

    在没有学习Hibernate之前,我们一直都是用jdbc来连接数据库和操纵数据库.所以在刚接触Hibernate时,我们都有一个疑问,为什么要学Hibernate,jdbc不是挺好的吗?那么接下来就来 ...

  2. C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】

    STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...

  3. Android MenuItem 设置文本颜色-TextColor设置

    前面一直在寻找 MenuItem文字颜色设置. 我发现API唯一的背景颜色设置. .. 因此,找到下面的方法.在OverFlow看到. 在onCreateOptionsMenu一下. 使MenuIte ...

  4. CentOS6.5查看一port执行状态

    netstat -nap | grep 22 版权声明:本文博主原创文章,博客,未经同意不得转载.

  5. JQuery插件开发初探——结构熟悉

    工作之余,对Jquery插件做了一点尝试,想着之前总用别人写的插件,自己要是也写一个用岂不是很cool.于是说干就干,动手开始写. 首先是模仿,从一个简单的功能进行入手,了解一下插件开发的流程和结构. ...

  6. thoughtworks笔试整理

    笔试了,时间1个半小时.没想到居然有7/10是开放性问题.大意例如以下:1.为什么选择增加ThoughtWorks.200字以内,不能用"interesting"."ch ...

  7. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  8. js手机对应的多级导航分享

    js移动导航对应,您可以使用自适应时屏幕,当小画面在一定程度上的网站.使导航出现,The navigation effects such as the following figures:多级导航! ...

  9. HDU 4005 The war (图论-tarjan)

    The war Problem Description In the war, the intelligence about the enemy is very important. Now, our ...

  10. C#-简单的定时器(C# ConsoleApp) ---ShinePans

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hpbmVwYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...