题目
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 为数组中
S
和E
中间路径上.
的数量,其中 0 < n < r ∗ c 0 < n < r * c 0<n<r∗c。我们可以找到一种最坏情况,即所有的点都是.
,都可以继续去向后遍历。此时路径数为 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(r∗c),其中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 为数组中
S
和E
中间路径上.
的数量,其中 0 < n < r ∗ c ∗ l 0 < n < r * c * l 0<n<r∗c∗l。我们可以找到一种最坏情况,即所有的点都是.
,都可以继续去向后遍历。此时路径数为 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(r∗c∗l),其中r为行数,c为列数,l为深度。主要为栈空间、visit、dp的开销。
ps:
个人理解,有问题一起讨论