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[ ...
随机推荐
- c++学习书籍推荐《Advanced C++》下载
百度云及其他网盘下载地址:点我 作者简介 James Coplien先在威斯康星大学获得电气与计算机工程学士学位,后又在该大学获得计算机科学硕士学位.他在贝尔实验室的软件产品研发部门工作,在这个部门从 ...
- wcf服务编程(二)
地址: 1.命名管道:用于同一台机器的跨进程通讯.URL表示方式为:net.pipe:// ;由于是在同一台机器的不同进程间通讯,所以不用定义端口号.
- extern和static区别
1. 声明和定义 当定义一个变量的时候,就包含了对该变量声明的过程,同时在内存张申请了一块内存空间.如果在多个文件中使用相同的变量,为了避免重复定义,就必须将声明和定义分离开来.定义是创建与名字关 ...
- SpringCloud解析之Eureka
本文基于Spring Cloud Edgware.SR6版本,从功能和架构上解析Eureka,让大家对Eureka有一个较为清晰的认识(本文默认大家对分布式微服务有一个初步的概念和理解,本文不涉及或少 ...
- JavaScript捕获与冒泡与委托
事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件. 相反的,事件冒泡是自下而上的去触发事件. 并不是所有的事件都能冒泡,以下事件不冒泡:blur.focus.load.un ...
- java Springboot 生成 二维码 +logo
上码,如有问题或者优化,劳请广友下方留言 1.工具类 import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHint ...
- bzoj1854 游戏题解(二分图/并查集)
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5547 Solved: 2229[Submit][Status] ...
- 后端开发工具:反编译工具、VS插件、.NET Framework源码地址
再学习.工作中,开发免不了要使用第三方工具.今天介绍2款反编译工具 一.dnspy 免安装.免费.可调试.可修改重新编译dll 开源项目地址:https://github.com/0xd4d/dnSp ...
- 新手小白之学习python一飞冲天日志之—基本数据类型,条件控制语句
python的历史 04年目前最流行的WEB框架Django诞生 python2:源码不统一,有重复的功能代码 python3:源码统一,没有重复的功能代码 python是一个什么编程语言 编译型:编 ...
- 初识nginx!
What--什么是nginx nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.官方测试nginx能够支撑5w并发连接.并且cup.内存等资源消耗却非常 ...