Description
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?
Input
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.
Output
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!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
分析
题目的大概意思就是在一栋大楼里面有很多的秘密通道,S是起始位置,E是出口,'#'代表的是墙, '.' 代表的是路,问由S到E所要经过的最短时间。
BFS题目。该题目和迷宫不同的是,迷宫是平面上的东南西北的移动,相当于在大楼里面的一层中找出口,而该题目在迷宫基础上增加了上下的移动,即在大楼里的上下层之间的移动,需要建立三维数组,找到S的位置,移动方向也由原来的四个增加到6个,知道找到出口E为止,若找遍所有的能走的地方没有找到出口E,则就出不来了。
代码如下:
public class BTest {
//移动方向,前后左右上下六个方向坐标
private static final int[][] dir = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
private static volatile int time = -1;
//存储输入的内容
private static final char[][][] arr = new char[40][40][40];
//记录访问过的坐标
private static final int[][][] visited = new int[40][40][40];
private static int L, R, C, sx, sy, sz, ex, ey, ez;
//队列
private static final LinkedList<String> list = new LinkedList<String>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
String[] numStr = scanner.nextLine().split(" ");
L = Integer.valueOf(numStr[0]);
R = Integer.valueOf(numStr[1]);
C = Integer.valueOf(numStr[2]);
for (int i = 0; i < L; i++){
for (int j = 0; j < R; j++){
String[] tmp = scanner.nextLine().split(" ");
for (int k = 0; k < C; k++){
arr[i][j][k] = tmp[k].toCharArray()[0];
if (arr[i][j][k] == 'S'){
sx = i;
sy = j;
sz = k;
}
if (arr[i][j][k] == 'E'){
ex = i;
ey = j;
ez = k;
}
}
}
}
scanner.close();
BFSSearch();
}
private static void BFSSearch(){
//标记记录已经被访问过
visited[sx][sy][sz] = 1;
//入队
list.add(sx + "|" + sy + "|" + sz);
int l, r, c;
while (!list.isEmpty()){
String[] tmp = list.getFirst().split("|");
list.removeFirst();
l = Integer.parseInt(tmp[0]);
r = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
if (l == ex && r == ey && c == ez){
System.out.println("Escaped in " + time + " minute!");
return;
}
//遍历六个方向
for (int i = 0; i < 6; i++){
l += dir[i][0];
r += dir[i][1];
c += dir[i][2];
if (!judge(r, c, l)){
continue;
}
time++;
if (visited[l][r][c] == 0){
visited[l][r][c] = 1;
list.add(l + "|" + r + "|" + c);
}
}
}
System.out.println("Trapped!");
}
private static boolean judge(int r, int c, int l){
if (r >= 0 && r < R && c >= 0 && c < C && l >= 0 && l < L
&& arr[l][r][c] != '#') return true;
return false;
}
}
本文介绍了一种基于BFS的3D迷宫逃脱算法,通过三维数组模拟迷宫结构,实现从起点到出口的最短路径寻找。文章详细解释了算法流程,包括如何处理六向移动和判断路径有效性。
432

被折叠的 条评论
为什么被折叠?



