import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack; public class BFS
{
private static final int M=3;//代表行
private static final int N=3;//代表列 int destX=1;
int destY=0; //int[][] maze={{0,0,1,1},{0,0,0,1},{1,1,0,1},{0,0,0,0}};//迷宫布局,1表示障碍物
int[][] maze={{0,0,0},{0,1,0},{0,0,0}};//迷宫布局,1表示障碍物
int[][] visit=new int[M][N];//标记是否已经访问过
int[][] stepArr={{-1,0},{1,0},{0,-1},{0,1}};//表示走路的方向:上下左右 class Node
{
int x,y;//表示当前位置的坐标
int step;
int preX,preY; Node(int x,int y,int preX,int preY,int step)
{
this.x=x;//表示当前位置的横坐标
this.y=y;//表示当前位置的纵坐标
this.preX=preX;//表示当前位置前一步的横坐标
this.preY=preY;//表示当前位置前一步的纵坐标
this.step=step;//表示行走的路程
}
} public boolean bfs()
{
Node node=new Node(0,0,-1,-1,0);//起始位置
Queue<Node> queue=new LinkedList<Node>();//LinkedList类实现了Queue接口
Stack<Node> stack=new Stack<Node>(); queue.offer(node);//插入node while(!queue.isEmpty())
{
Node head=queue.poll();//推出队列头 stack.push(head); //用于保存路径
visit[head.x][head.y] = 1; //表示当前位置已被访问,防止两个坐标循环访问
for(int i = 0; i < 4; i++){ //这里的4表示4张方向
int x = head.x + stepArr[i][0];
int y = head.y + stepArr[i][1];
//exit
if(x ==destX && y == destY && maze[x][y] == 0 && visit[x][y] == 0){ //表示找到目的地
//打印路径
Node top = stack.pop();
System.out.println("steps:" + (top.step + 1)); //此处得出的路径是最短路径,因为是queue中保存了每一种可能
System.out.println("the path:");
System.out.println((M - 1) + "," + (N - 1));
System.out.println(top.x + "," + top.y);
int preX = top.preX;
int preY = top.preY;
while(!stack.isEmpty()){
top = stack.pop();
if (preX == top.x && preY == top.y) { // 因为队列中保存了每一次的各种可能,故需要与之前保存的点进行验证
System.out.println(preX + "," + preY);
preX = top.preX;
preY = top.preY;
} }
return true;
}
//bfs
if(x >= 0 && x < M && y >= 0 && y < N &&maze[x][y] == 0 && visit[x][y] == 0){
Node newNode = new Node(x, y, head.x, head.y, head.step + 1);
queue.offer(newNode);
}
} }
return false;
} public static void main(String[] args)
{
BFS b=new BFS();
if (b.bfs()==false)
{
System.out.println("Fail!");
}
} }

利用bfs可求出迷宫的最短路径。

如果使用dfs的话只能判断能否走出迷宫,并不能知道所走路径是否为最短路径。

迷宫问题(bfs)的更多相关文章

  1. ZZULIOJ 1726 迷宫(BFS+小坑)

    1726: 迷宫 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 394  Solved: 64 SubmitStatusWeb Board Descr ...

  2. HDU 1728 逃离迷宫(BFS)

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  3. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  4. 迷宫问题(bfs的应用)

    问题描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...

  5. [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)

    题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  6. 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)

    题目链接:https://www.dotcpp.com/oj/problem1923.html 题目描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在 ...

  7. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  8. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  9. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

随机推荐

  1. vmware12安装vmtools

    一. 1. 机器要开启支持BIOS的选项. 二. 1. 点击vmware 应用 vmware install 2. 虚拟机: tar  xvf vmtools-distb.tgr.gz 3. 虚拟机: ...

  2. 3016 质子撞击炮 II

    3016 质子撞击炮 II  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description [抱歉数据错误~~已修 ...

  3. 后台跳转到登录页嵌套在iframe的问题(MVC例)

    //首页 public ActionResult Index() { if (!Request.IsAuthenticated) //判断权限,没有登录就跳回登录页 {string url = Url ...

  4. es安装

    1,安装java(至少1.8) yum install -y java java -version 在/etc/profile追加: JAVA_HOME=/usr/java/jdk1..0_45 PA ...

  5. [shell]. 点的含义

    . 的含义 当前目录 隐藏文件 任意一个字符 生效配置文件

  6. 深入分析 Javascript 单线程

    面试的时候发现99%的童鞋不理解为什么JavaScript是单线程的却能让AJAX异步发送和回调请求,还有setTimeout也看起来像是多线程的?还有non-blocking IO, event l ...

  7. python 反模式

    不使用 pythonic 的循环: l = [1,2,3] #Bad for i in range(0,len(list)): le = l[i] print(i,le) #Good for i,le ...

  8. Gt9xx芯片,在规格书+Linux驱动的基础上,移植为USB裸机经验。直接用开发板,不去碰硬件的坑。

    1,用内核代码和规格书来印证数据格式: //命令3字节,IC地址 u8 end_cmd[] = {GTP_READ_COOR_ADDR >> , GTP_READ_COOR_ADDR &a ...

  9. Sublime Text 之 Package Control 镜像

    本文同步自我的个人博客:http://www.52cik.com/2015/11/24/Package-Control.html 这阵子经常有朋友跟我说 Sublime Text 下的 Package ...

  10. 第十章 使用MapKit

    本项目是<beginning iOS8 programming with swift>中的项目学习笔记==>全部笔记目录 ------------------------------ ...