题目描述
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?
你进入了一个3D的宝藏地宫中探寻宝藏到了宝藏,你可以找到走出地宫的路带出宝藏,或者使用炉石空手回家。
地宫由立方体单位构成,立方体中不定会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能对角线移动并且地宫四周坚石环绕。
请问你是否可以走出地宫带出宝藏?如果存在,则需要多少时间?
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.
每个地宫描述的第一行为L,R和C(皆不超过30)。
L表示地宫的层数。
R和C分别表示每层地宫的行与列的大小。
随后L层地宫,每层R行,每行C个字符。
每个字符表示地宫的一个单元。'#'表示岩石单元,'.'表示空白单元。你的起始位置在'S',出口为'E'。
每层地宫后都有一个空行。L,R和C均为0时输入结束。
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!
每个地宫对应一行输出。
如果可以带出宝藏,则输出
Escaped in x minute(s).
x为最短脱离时间。
如果无法带出,则输出
Trapped!
Sample
Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Output
Escaped in 11 minute(s).
Trapped!
bfs(3D)
static class TaskA {
static int[] bx = {0,0,1,-1,0,0};
static int[] by = {-1,1,0,0,0,0};
static int[] bz = {0,0,0,0,-1,1};
int sx,sy,sz;
int ex,ey,ez;
static class node{
int x,y,z;
int sum;
public node(int x,int y,int z,int sum){
this.x = x;
this.y = y;
this.z = z;
this.sum = sum;
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
while (in.hasNext()) {
int l = in.nextInt();
int r = in.nextInt();
int c = in.nextInt();
int[][][] vis = new int[r][c][l];
char[][][] a = new char[r][c][l];
Queue<node> q = new LinkedList();
if(l == 0&&r == 0&&c == 0){//L、R、C均为0时输入结束
break;
}
for (int i = 0; i < l; i++) {
for (int j = 0; j < r; j++) {
String ss = in.next();
for (int k = 0; k < c; k++) {
a[j][k][i] = ss.charAt(k);
if (a[j][k][i] == 'S') {//记录起点坐标
sx = j;
sy = k;
sz = i;
}
if (a[j][k][i] == 'E') {//记录终点坐标
ex = j;
ey = k;
ez = i;
}
}
}
}
q.offer(new node(sx,sy,sz,0));
vis[sx][sy][sz] = 1;
int sum = 0;
int f = 0;
while (!q.isEmpty()){
node n = q.poll();
if(n.x == ex && n.y == ey && n.z == ez){//走到终点,可以走出迷宫
sum = n.sum;
f = 1;
break;
}else{
for (int i = 0; i < 6; i++) {//上下、前后、左右六个方向
int x = n.x + bx[i];
int y = n.y + by[i];
int z = n.z + bz[i];
if(x >= 0&& x <= r - 1&&y >= 0&& y <= c -1 &&z >= 0&& z <= l - 1) {//不超限
if (a[x][y][z] != '#') {//可以走
if (vis[x][y][z] == 0) {//没走过
q.offer(new node(x, y, z, n.sum+1));
vis[x][y][z] = 1;
}
}
}
}
}
}
if (f == 0) {//走不出迷宫
out.println("Trapped!");
} else {
out.println("Escaped in " + sum + " minute(s).");
}
}
}
}