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. transition的局限

    transition的优点在于简单易用,但是它有几个很大的局限. (1)transition需要事件触发,所以没法在网页加载时自动发生. (2)transition是一次性的,不能重复发生,除非一再触 ...

  2. UOJ 151 斗地主“加强”版

    #151. [NOIP2015]斗地主“加强”版 统计 描述 提交 自定义测试 本题开放Hack 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54 ...

  3. 【转】【UML】使用Visual Studio 2010 Team System中的架构师工具(设计与建模)

    Lab 1: 应用程序建模 实验目标 这个实验的目的是展示如何在Visual Studio 2010旗舰版中进行应用程序建模.团队中的架构师会通过建模确定应用程序是否满足客户的需求. 你可以创建不同级 ...

  4. 微软职位内部推荐-Software Development Engineer 2

    微软近期Open的职位: SDE II Organization Summary: Engineering, Customer interactions & Online (ECO) is l ...

  5. VMware 不可恢复错误(svga)”解决方法

    虚拟机VMware 文件在迁移到另一台计算机时出现"VMware Workstation 不可恢复错误(svga)"  将另一台机器的 VMware 文件拷贝至本机,打开虚拟机出现 ...

  6. ReactNative之style使用指南

    ReactNative中能使用的css样式有哪些呢Valid style props: [   "alignItems",   "alignSelf",   & ...

  7. chrome http Request Header 修改插件

    chrome http Request Header 修改插件 2013-05-31 11:03:03|  分类: JavaScript |  标签:chrome  extensions  chang ...

  8. SpringMVC数据验证

    SpringMVC数据验证——第七章 注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC 资源来自:http://jinnianshilongnian.iteye.com/blo ...

  9. 关于sql 的convert 格式设置

    CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式) 说明:此样式一般在时间类型(datetime,smalldate ...

  10. 解决BeanNotOfRequiredTypeException: Bean named 'XXX' must be of type XXX, but was actually of type XXX问题

    Java新手,困扰了一下午. 发布时总是报这样一个错误. org.springframework.beans.factory.BeanCreationException: Error creating ...