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. xshell5 启动显示 mfc110.dll msvcp110.dll 未找到问题 解决办法

    1. 安装 Visual C++ Redistributable for Visual Studio 2012 x86版本 注意: 一定要安装x86版本.(xshell5是32位的程序) 微软的官方下 ...

  2. js模拟手机触摸屏

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

    原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...

  4. 类图和对象图教程-类(Class)、接口(Interface)、协作(collaboration)、依赖关系(Dependency)、泛化关系(Generalization)、关联关系(Association)以及实现关系(Realization)

    类图的概念 (转) 一.概述 类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图基础上,可以使用状态图.协作图. ...

  5. Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  6. C++ c++初识

    //c++初识 #include<iostream> //包含c++标准头文件 //<iostream>和<iostream.h>格式不一样,前者没有后缀,实际上, ...

  7. U3D5.3.5f Monodevelop 仅支持到.NET 3.5

    2016年12月2号:发现这个标题是错误的,可以在monodevelop中选择.NET的版本,如下:打开solution,右击 Assembly-CSharp,options, build, gene ...

  8. 从0开始学Java——JSP和Servlet——jsp转servlet出错的三个典型场景

    由于jsp终究是要转换为servlet的java文件,然后再编译为.class文件,最后才执行,那么在这过程的任何一个步骤都可能有问题,主要包括三个方面,下面逐一分析: 一.JSP转换为Servlet ...

  9. 20145208 《Java程序设计》第6周学习总结

    20145208 <Java程序设计>第6周学习总结 教材学习内容总结 输入与输出 InputStream与OutputStream 从应用程序角度来看,如果要将数据从来源取出,可以使用输 ...

  10. windows 7 + vs2010 sp1编译 x64位版qt4

    由于qt官方没有发布预编译的64位版qt4,要使用64位版qt4,只能自己编译,编译过程如下: 1,下载源码并解压到D:\qt-src\qt-everywhere-opensource-src-4.8 ...