package myalgorithm;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/*BFS用于记录的位置和值的结构*/
class node
{
node(int xparam,int yparam,int valparam)
{
this.x = xparam;
this.y = yparam;
this.value = valparam;
}
int x,y,value;
}
public class ShortPath {
/*全局最短路径*/
public int stepnum = 999;
/*构建11*11的迷宫,英雄H在(1,1)的位置出发,去解救美女M(6,8)*/
char[][] graph = {
{'#','#','#','#','#','#','#','#','#','#','#'},
{'#','H','_','_','*','_','_','*','_','_','#'},
{'#','_','_','_','_','_','_','_','_','_','#'},
{'#','_','*','_','_','_','*','_','_','_','#'},
{'#','_','_','_','*','_','_','_','_','*','#'},
{'#','_','_','_','_','_','_','*','_','*','#'},
{'#','_','*','_','_','_','_','_','M','_','#'},
{'#','*','_','_','*','_','_','_','_','_','#'},
{'#','_','_','_','_','_','_','_','_','_','#'},
{'#','_','_','_','*','_','_','_','_','_','#'},
{'#','#','#','#','#','#','#','#','#','#','#'},
};
/*初始标记数组都为0*/
public int[][] mark = new int[graph.length][graph.length];
/*每一个位置有四种选择:右下左上*/
public int[][] choose = {
{0,1},
{1,0},
{0,-1},
{-1,0}
};
/*BFS算法*/
public void BFS(node startPoint)
{
//起始点装入队列
Queue<node> queue = new LinkedList<node>();
startPoint.value = 1;//确保起始步数为1
queue.offer(startPoint); node t1;
int tx,ty;
top:
while(!queue.isEmpty())
{
//取队首,出队后不再入队,value也自此固定
t1 = queue.poll();
mark[t1.x][t1.y] = t1.value;//标记步数
for(int i=0;i<4;i++)
{
tx = t1.x + choose[i][0];
ty = t1.y + choose[i][1]; //找到美女,肯定是最短的,可以立即返回
if(graph[tx][ty] == 'M')
{
stepnum = t1.value + 1;//下一步可到
mark[tx][ty] = stepnum;
break top;
}
//继续接着找,把空路径添加到队列末尾
//不是炸弹和围墙,并且没有被标记
if(graph[tx][ty] != '#'
&& graph[tx][ty] != '*'
&&mark[tx][ty] == 0)
{
queue.offer(new node(tx,ty,t1.value+1));
}
}
}
}
/*BFS回溯路径*/
private void getPath(node target) { int beforex = 0,beforey = 0;
int x = target.x;
int y = target.y;
for(int i=0;i<4;i++)
{
beforex = x + choose[i][0];
beforey = y + choose[i][1];
//找到英雄的出发点,函数结束
if (beforex == 1 && beforey == 1)
{
return;
}
if(mark[x][y] - 1 == mark[beforex][beforey])
{
System.out.print("("+beforex+","+beforey+")<--");
break;
}
}
getPath(new node(beforex,beforey,0));
return;
}
/*main函数*/
public static void main(String[] args) {
ShortPath my = new ShortPath(); long start = System.currentTimeMillis();
my.BFS(new node(1,1,0));
long end = System.currentTimeMillis();
System.out.println("BFS step: " + my.stepnum + " time:" + (end-start));
//打印标记结果
for (int m = 0;m<my.graph.length;m++)
System.out.println(Arrays.toString(my.mark[m]));
//打印巡回路径
my.getPath(new node(6,8,0));
} }

BFS step: 13 time:4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 7, 8, 0, 10, 11, 0]
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[0, 3, 0, 5, 6, 7, 0, 9, 10, 11, 0]
[0, 4, 5, 6, 0, 8, 9, 10, 11, 0, 0]
[0, 5, 6, 7, 8, 9, 10, 0, 12, 0, 0]
[0, 6, 0, 8, 9, 10, 11, 0, 13, 0, 0]
[0, 0, 10, 9, 0, 11, 0, 0, 0, 0, 0]
[0, 0, 11, 10, 11, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(5,8)<--(4,8)<--(4,7)<--(4,6)<--(4,5)<--(3,5)<--(3,4)<--(3,3)<--(2,3)<--(2,2)<--(2,1)


扩展:

PhotoShop中的魔术棒选择工具的原理,就是从鼠标选中的点作为种子,并加入队列;依次查找周边的点,如果颜色与种子颜色相近,则加入队列;对队列中的元素依次进行类似扩展,这样就选取了与最初种子颜色相同的点的集合。

最新文章

  1. [转] 有java基础的人如何转行做大数据?
  2. Java03
  3. jquery 跨域访问问题 转
  4. Java中常用修饰符使用汇总
  5. 【jQuery】Jquery.cookie()
  6. bootstrap的验证和确认对话框
  7. 按钮打开链接,按钮click代码
  8. 一个包的libevent流程
  9. 【不积跬步,无以致千里】安装roundcube 时出现 “DSN (write): NOT OK(SQLSTATE[HY000] [2002] No such file or directory)”
  10. Parallels Desktop 7用bootcamp安装win7 后如何激活WIN7
  11. 【UVA10829】 L-Gap Substrings (后缀数组)
  12. 线性链表的双向链表——java实现
  13. 基于visual Studio2013解决C语言竞赛题之0524职工年龄
  14. Crazy Calendar (阶梯博弈变形)
  15. Build to win
  16. 【原创】大叔问题定位分享(2)spark任务一定几率报错java.lang.NoSuchFieldError: HIVE_MOVE_FILES_THREAD_COUNT
  17. 前端开发需要掌握的SEO的知识点
  18. 主流JS库一览
  19. Django_缓存
  20. Android Studio 创建/打开项目时一直处于Building“project name”Gradle project info 的解决

热门文章

  1. php中数组自定义排序
  2. Python学习 之 switch语句
  3. MySQL在Django框架下的基本操作(MySQL在Linux下配置)
  4. 修改MySQL的时区
  5. C#打开指定目录,并将焦点放在指定文件上。相对路径(程序起动的目录)
  6. tomcat部署应用的几种方式
  7. HTML+CSS 实现水流流动效果
  8. Git CMD - rm: Remove files from the working tree and from the index
  9. HDOJ2018母牛的故事
  10. Ajax-数据格式-xml,json