【专题一 简单搜索】2. Dungeon Master

题目

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

你被困在一个 L×R×C 的三维迷宫内,每分钟你只能够往上、下、左、右、前、后这六个方向的任意一个方向走一个单位。初始时你位于 S 位置,迷宫的出口为 E 位置,迷宫中标 # 的位置无法通过,并且你也不能够走出迷宫的边界。

你是否能够走出这个迷宫?如果能,求出最少的时间。

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.

迷宫有 L 层,每层长宽为 R×C。(L,R,C≤30)

输入有多组,以 L=R=C=0 结束。

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

输入

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

输出

11
-1

思路

题目为3d找路线,因为3d有些复杂,所以我选择先把2d写出来

转换为2d

设定输入的迷宫如下:

char[][] maze = new char[][]{
        {'S', '.', '.', '.'},
        {'.', '#', '#', '.'},
        {'.', '.', '.', '.'},
        {'#', '#', '.', '#'},
        {'#', '#', '.', 'E'}
};

visit用来存储是否访问过,dp用来存储到达当前节点的最少时间,为了方和dp初始化时的其他元素作区分,dp[0][0]做特殊处理,设置花费时间为1

每一个位置可以由上下左右四个位置移动得到,所以当前位置的最小值为min(dp[i - 1][j], dp[i + 1][j], dp[i][j - 1], dp[i][j + 1]) + 1

在这里插入图片描述

深度遍历,遍历完一个位置之后,又可以向上下左右四个方向移动,遇到#直接返回,否则就继续向下移动,通过getMin方法,来得到从上下左右四个方向的最短时间,直到遇到E结束

代码


public class t1_q2_Dungeon_Master_2d {

    public static void main(String[] args) {
        Sloution s = new Sloution();
        char[][] maze = new char[][]{
                {'S', '.', '.', '.'},
                {'.', '#', '#', '.'},
                {'.', '.', '.', '.'},
                {'#', '#', '.', '#'},
                {'#', '#', '.', 'E'}
        };
        int time = s.func(maze);
        System.out.println(time);
    }

    static class Sloution {

        char[][] maze;
        int[][] visit;
        int[][] dp;
        int r, c;
        int res = -1;

        public int func(char[][] maze) {
            this.maze = maze;
            this.r = maze.length;
            this.c = maze[0].length;
            this.visit = new int[r][c];
            this.dp = new int[r][c];
            bfs(0, 0);
            return res;
        }

        public void bfs(int i, int j) {
            if (i < 0 || i >= r || j < 0 || j >= c) return;
            if (maze[i][j] == '#') return;
            if (maze[i][j] == 'E') res = getMin(i, j);
            if (maze[i][j] != '#' && visit[i][j] == 0) {
                visit[i][j] = 1;
                if (i == 0 && j == 0) dp[i][j] = 1;
                else dp[i][j] = getMin(i, j) + 1;
                bfs(i + 1, j);
                bfs(i, j + 1);
                bfs(i - 1, j);
                bfs(i, j - 1);
                visit[i][j] = 0;
            }
        }

        int getMin(int i, int j) {
            int min = Integer.MAX_VALUE;
            if (i - 1 >= 0 && maze[i][j] != '#' && dp[i - 1][j] > 0) min = Math.min(dp[i - 1][j], min);
            if (i + 1 < r && maze[i][j] != '#' && dp[i + 1][j] > 0) min = Math.min(dp[i + 1][j], min);
            if (j - 1 >= 0 && maze[i][j] != '#' && dp[i][j - 1] > 0) min = Math.min(dp[i][j - 1], min);
            if (j + 1 < c && maze[i][j] != '#' && dp[i][j + 1] > 0) min = Math.min(dp[i][j + 1], min);
            return min;
        }
    }
}
  • 时间复杂度: O ( n 2 ) O(n^{2}) O(n2),其中 n 为数组中SE中间路径上.的数量,其中 0 < n < r ∗ c 0 < n < r * c 0<n<rc。我们可以找到一种最坏情况,即所有的点都是.,都可以继续去向后遍历。此时路径数为 O ( 2 n ) O(2^{n}) O(2n),且每条路径长度为 O(n),因此总时间复杂度为 O ( n 2 ) O(n^{2}) O(n2)

  • 空间复杂度: O ( r ∗ c ) O(r * c) O(rc),其中r为行数,c为列数。主要为栈空间、visit、dp的开销。

对于3d

2d维护了二维数组,所以三维就把数组换为3d,二维数组有上下左右四个方向,三维数组有上下左右前后四个方向

visit用来存储是否访问过,dp用来存储到达当前节点的最少时间,为了方和dp初始化时的其他元素作区分,dp[0][0][0]做特殊处理,设置花费时间为1

在这里插入图片描述

每一个位置可以由上下左右前后六个位置移动得到,所以当前位置的最小值为min(dp[i - 1][j][k], dp[i + 1][j][k], dp[i][j - 1][k], dp[i][j + 1][k], dp[i][j][k - 1], dp[i][j][k + 1] ) + 1

同上,深度遍历,遍历完一个位置之后,又可以向上下左右前后六个方向移动,遇到#直接返回,否则就继续向下移动,通过getMin方法,来得到从上下左右前后六个方向的最短时间,直到遇到E结束

代码


public class t1_q2_Dungeon_Master_3d {

    public static void main(String[] args) {
        Sloution s = new Sloution();
        char[][][] maze = new char[][][]{
                {
                        {'S', '.', '.', '.', '.'},
                        {'.', '#', '#', '#', '.'},
                        {'.', '#', '#', '.', '.'},
                        {'#', '#', '#', '.', '#'}
                },
                {
                        {'#', '#', '#', '#', '#'},
                        {'#', '#', '#', '#', '#'},
                        {'#', '#', '.', '#', '#'},
                        {'#', '#', '.', '.', '.'}
                },
                {
                        {'#', '#', '#', '#', '#'},
                        {'#', '#', '#', '#', '#'},
                        {'#', '.', '#', '#', '#'},
                        {'#', '#', '#', '#', 'E'}
                }
        };
        int time = s.func(maze);
        System.out.println(time);

        char[][][] maze1 = new char[][][]{
                {
                        {'S', '#', '#'},
                        {'.', '#', '#'},
                        {'.', '#', 'E'}
                }
        };
        int time1 = s.func(maze1);
        System.out.println(time1);
    }

    static class Sloution {

        char[][][] maze;
        int[][][] visit;
        int[][][] dp;
        int res = -1;
        int l, r, c;

        public int func(char[][][] maze) {
            this.maze = maze;
            this.l = maze.length;
            this.r = maze[0].length;
            this.c = maze[0][0].length;
            this.visit = new int[l][r][c];
            this.dp = new int[l][r][c];
            bfs(0, 0, 0);
            return res;
        }

        public void bfs(int i, int j, int k) {
            if (i < 0 || i >= l || j < 0 || j >= r || k < 0 || k >= c) return;
            if (maze[i][j][k] == '#') return;
            if (maze[i][j][k] == 'E') {
                res = res == -1 ? getMin(i, j, k) : Math.min(res, getMin(i, j, k));
            }
            if (maze[i][j][k] != '#' && visit[i][j][k] == 0) {
                visit[i][j][k] = 1;
                if (i == 0 && j == 0 && k == 0) dp[i][j][k] = 1;
                else dp[i][j][k] = getMin(i, j, k) + 1;
                bfs(i + 1, j, k);
                bfs(i, j + 1, k);
                bfs(i, j, k + 1);
                bfs(i - 1, j, k);
                bfs(i, j - 1, k);
                bfs(i, j, k - 1);
                visit[i][j][k] = 0;
            }
        }

        int getMin(int i, int j, int k) {
            int min = Integer.MAX_VALUE;
            if (i - 1 >= 0 && maze[i][j][k] != '#' && dp[i - 1][j][k] > 0) min = Math.min(dp[i - 1][j][k], min);
            if (i + 1 < l && maze[i][j][k] != '#' && dp[i + 1][j][k] > 0) min = Math.min(dp[i + 1][j][k], min);
            if (j - 1 >= 0 && maze[i][j][k] != '#' && dp[i][j - 1][k] > 0) min = Math.min(dp[i][j - 1][k], min);
            if (j + 1 < r && maze[i][j][k] != '#' && dp[i][j + 1][k] > 0) min = Math.min(dp[i][j + 1][k], min);
            if (k - 1 >= 0 && maze[i][j][k] != '#' && dp[i][j][k - 1] > 0) min = Math.min(dp[i][j][k - 1], min);
            if (k + 1 < c && maze[i][j][k] != '#' && dp[i][j][k + 1] > 0) min = Math.min(dp[i][j][k + 1], min);
            return min;
        }
    }
}
  • 时间复杂度: O ( n 3 ) O(n^{3}) O(n3),其中 n 为数组中SE中间路径上.的数量,其中 0 < n < r ∗ c ∗ l 0 < n < r * c * l 0<n<rcl。我们可以找到一种最坏情况,即所有的点都是.,都可以继续去向后遍历。此时路径数为 O ( 3 n ) O(3^{n}) O(3n),且每条路径长度为 O(n),因此总时间复杂度为 O ( n 3 ) O(n^{3}) O(n3)

  • 空间复杂度: O ( r ∗ c ∗ l ) O(r * c * l) O(rcl),其中r为行数,c为列数,l为深度。主要为栈空间、visit、dp的开销。

ps:
个人理解,有问题一起讨论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Geek-Banana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值