问题描述

定义一个二维数组: 

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-迷宫问题的更多相关文章

  1. bfs—迷宫问题—poj3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20591   Accepted: 12050 http://poj ...

  2. uva 816 - Abbott&#39;s Revenge(有点困难bfs迷宫称号)

    是典型的bfs,但是,这个问题的目的在于读取条件的困难,而不是简单地推断,需要找到一种方法来读取条件.还需要想办法去推断每一点不能满足条件,继续往下走. #include<cstdio> ...

  3. BFS迷宫搜索路径

    #include<graphics.h> #include<stdlib.h> #include<conio.h> #include<time.h> # ...

  4. HDU2579(bfs迷宫)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. bfs迷宫

    链接:https://ac.nowcoder.com/acm/contest/338/BSleeping is a favorite of little bearBaby, because the w ...

  6. BFS迷宫问题

    链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...

  7. 【OpenJ_Bailian - 2790】迷宫(bfs)

    -->迷宫  Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...

  8. ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)

    题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...

  9. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  10. 3299: [USACO2011 Open]Corn Maze玉米迷宫

    3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[ ...

随机推荐

  1. Redis HyperLogLog用法简介

    (1)HyperLogLog简介 在Redis 在 2.8.9 版本才添加了 HyperLogLog,HyperLogLog算法是用于基数统计的算法,每个 HyperLogLog 键只需要花费 12 ...

  2. 后端开发工具:反编译工具、VS插件、.NET Framework源码地址

    再学习.工作中,开发免不了要使用第三方工具.今天介绍2款反编译工具 一.dnspy 免安装.免费.可调试.可修改重新编译dll 开源项目地址:https://github.com/0xd4d/dnSp ...

  3. k8s学习 - 概念 - ReplicaSet

    k8s学习 - 概念 - ReplicaSet 首先,ReplicaSet 和 ReplicationController 基本上一样,除了上篇说到的selector有不同之外,没有啥区别.(官网也是 ...

  4. 【bfs】密码锁-C++

    Description 现在一个紧急的任务是打开一个密码锁.密码由四位数字组成,每个数字从 1 到 9 进行编号.每次可以对任何数字加 1 或减 1.当将9加 1 时,数字将变为1,当1减 1 的时, ...

  5. ADO.NET_包括DataReader和dataSet的使用

    今天总结了一下ADO.NET编程中DataReader和dataSet两个比较重要的对象的使用,完成了combobox,listbox,以及fpSpread动态添加数据的测试,对使用sqlComman ...

  6. Web前端三大框架_vue源码笔记

    一.VUE 1.1 MVVM VUE也是基于MVVM模式实现的.特点就是数据双向绑定 在MVVM模式中,分成三个部分: M 模型 model V 视图 view VM 视图-模型 view-model ...

  7. html css 布局小细节

    学了两个月的html和css每天都重复一样的生活,敲着大同小异的代码,这样的生活枯燥无味.我腻了,我也累了!小米首页算是我写的第三个静态页面,写了好久,很多细节都把握不好,下面的这个简单的布局细节是我 ...

  8. app组件跳转到页面

    这段时间根据项目需求,开发一个app的一个页面,这里用到了从组件跳转到index文件下的.vue页面.第一次接触,参考了同事的文档,写出来了,这里记录一下. 文档链接: https://www.yuq ...

  9. 集群之间配置 SSH无密码登录

    集群之间配置 SSH无密码登录 配置 ssh (1)基本语法 ssh 另一台电脑的 ip 地址 (2)ssh 连接时出现 Host key verification failed 的解决方法 # ss ...

  10. Linux卸载MySql——ubuntu版

    卸载mysql 1)删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2)删除mqsql的配置文件 sudo rm /etc/mysql/ -R 3)自动卸载mysql的 ...