BFS-迷宫问题
问题描述
定义一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
代码
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Test16 {
// 用于记录当前节点
public static class Node {
int x;
int y;
int step;
}
static int[][] migong = new int[6][6];
// 记录防止重走
static boolean[][] vsd = new boolean[6][6];
// 记录上一步情况
static int[][] pre_step = new int[6][6];
// 用于层次遍历
static Queue<Node> queue = new LinkedList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
migong[i][j] = in.nextInt();
if (migong[i][j] == 1) {
vsd[i][j] = true;
}
}
}
Node node = new Node();
node.x = 0;
node.y = 0;
node.step = 0;
bfs(node);
}
// 思路:使用bfs思想,走到最后一步migong[4][4],不断记录上一步位置,到达最后位置后,退回去回初始位置migong[0][0],就是最短路径。
// 用变量1,2,3,4分别记录为上下左右,放进数组中用来控制变量
// 用一个数组记录上一步情况
// 用一个stack利用先进后出,输出路径
public static boolean cheak(Node node) {
if (node.x >= 0 && node.x < 5 && node.y >= 0 && node.y < 5 && !vsd[node.x][node.y]) {
return true;
} else {
return false;
}
}
// 方向控制
static int[][] dir = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
public static void bfs(Node node) {
// 使用两个node记录当前和下一个的状态
queue.offer(node);
Node now;
vsd[node.x][node.y] = true;
Node next = new Node();
while (!queue.isEmpty()) {
now = queue.peek();
// 控制方向 0右1左2下3上
for (int i = 0; i < 4; i++) {
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
if (cheak(next)) {
vsd[next.x][next.y] = true;
next.step = now.step + 1;
// 此处注意 一定要用零时Node加入队列中,因为java中对象是引用传递
Node tmp = new Node();
tmp.x = next.x;
tmp.y = next.y;
tmp.step = now.step + 1;
queue.offer(tmp);
pre_step[next.x][next.y] = i;
}
}
queue.poll();
if (now.x == 4 && now.y == 4) {
System.out.println("最短路径为:" + now.step);
break;
}
}
DisPlay();
}
// 输出所走路径
public static void DisPlay() {
Stack<Node> stack = new Stack<>();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(pre_step[i][j]);
}
System.out.println();
}
int i = 4, j = 4;
System.out.println("(" + i + "," + j + ")");
while (true) {
switch (pre_step[i][j]) {
case 0:
j = j - 1;
break;
case 1:
j = j + 1;
break;
case 2:
i = i - 1;
break;
case 3:
i = i + 1;
break;
default:
break;
}
System.out.println("(" + i + "," + j + ")");
if (i == 0 && j == 0) {
break;
}
}
}
}

BFS-迷宫问题的更多相关文章
- bfs—迷宫问题—poj3984
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20591 Accepted: 12050 http://poj ...
- uva 816 - Abbott's Revenge(有点困难bfs迷宫称号)
是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...
- BFS迷宫搜索路径
#include<graphics.h> #include<stdlib.h> #include<conio.h> #include<time.h> # ...
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- bfs迷宫
链接:https://ac.nowcoder.com/acm/contest/338/BSleeping is a favorite of little bearBaby, because the w ...
- BFS迷宫问题
链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...
- 【OpenJ_Bailian - 2790】迷宫(bfs)
-->迷宫 Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...
- ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)
题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...
- (BFS)poj2935-Basic Wall Maze
题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...
- 3299: [USACO2011 Open]Corn Maze玉米迷宫
3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 Solved: 59[ ...
随机推荐
- 基于SpringCloud的Microservices架构实战案例
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- ServiceFabric极简文档-5.1 编程模型选择
项目中:actor用的服务是无状态服务:ASP.NET Core用的是无状态ASP.NET Core模板.
- windows RDP远程代码执行_CVE-2019-0708漏洞复现
windows RDP远程代码执行_CVE-2019-0708漏洞复现 一.漏洞概述 2019年5月14日微软官方发布安全补丁,修复了windows远程桌面服务的远程代码执行漏洞,该漏洞影响了某些旧版 ...
- 用canvas绘制时钟
用canvas做时钟其实很简单,下面是我做出的效果: 是不是还挺漂亮的? 下面上代码: html <div class="whole"> <canvas id=& ...
- JS浅学
(变量的名字.focus(); )让打开的新的页面获取焦点 (变量的名字.close();)关闭打开的页面 可以用(!变量名)直接判断是否打开过新的页面 用(变量名.closed)判断是不是被关闭了 ...
- c语言进阶11-算法设计思想
一. 算法设计的要求: 为什么要学算法? /* 输出Hello word! */ #include "stdio.h" void main() { printf("He ...
- [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)
原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...
- java练习---9
//程序员:罗元昊 2017.10.22 package cn.lyh; import com.rupeng.game.GameCore; public class L implements Runn ...
- stack函数怎么用嘞?↓↓↓
c++ stl栈stack的头文件书写格式为: #include 实例化形式如下: stack StackName; 其中成员函数如下: 1.检验堆栈是否为空 empty() 堆栈为空则返回真 形式如 ...
- springBoot数据校验与统一异常处理
概念 异常,在程序中经常发生,如果发生异常怎样给用户一个良好的反馈体验就是我们需要处理的问题.以前处理异常信息,经常都是给前端一个统一的响应,如数据错误,程序崩溃等等.没办法指出哪里出错了,这是一种对 ...