zjm被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成。
zjm每次向上下前后左右移动一个单位需要一分钟,且zjm不能对角线移动。
空间的四周封闭。zjm的目标是走到空间的出口。
是否存在逃出生天的可能性?如果存在,则需要多少时间?
Input
输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度,R和C分别表示每层空间的行与列的大小。
随后L层,每层R行,每行C个字符。
每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。
zjm的起始位置在’S’,出口为’E’。每层空间后都有一个空行。
L,R和C均为0时输入结束。
Output
每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
Sample Input
3 4 5
S….
.###.
.##…
###.#
##.##
##…
#.###
####E
1 3 3
S##
#E#
0 0 0
Sample Outpu
Escaped in 11 minute(s).
Trapped!
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
struct point {
int x; int y; int z; int layer;
point(int x, int y, int z) :x(x), y(y), z(z) {
}
point(int x, int y, int z,int layer) :x(x), y(y), z(z),layer(layer) {
}
point() {
}
};
int ex = 0, ey = 0, ez = 0;
int px[] = { 1,0,0,0,0,-1 };
int py[] = { 0,1,0,0,-1,0 };
int pz[] = { 0,0,1,-1,0,0 };
int main() {
int l, r, c;
while (scanf("%d%d%d", &l, &r, &c) != EOF) {
int L[32][32][32] = { 0 };
int touch[32][32][32] = { 0 };
if (l == 0 && r == 0 && c == 0)break;
int sx = 0, sy = 0, sz = 0;
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= r; j++) {
for (int k = 1; k <= c; k++) {
char a;
cin >> a;
if (a == '#') continue;
else if (a == '.') L[i][j][k] = 1;
else if (a == 'S') {
sx = i;
sy = j;
sz = k;
}
else if (a == 'E') {
L[i][j][k] = 1;
ex = i;
ey = j;
ez = k;
}
}
}
}
queue<point> q;
q.push(point(sx, sy, sz,0));
touch[sx][sy][sz] = 1;
int layer = 0;
while (q.size() > 0) {
point* current = &q.front();
if (current->x == ex && current->y == ey && current->z == ez) {
layer = current->layer;
break;
}
for (int i = 0; i < 6; i++) {
if (L[px[i] + current->x][py[i] + current->y][pz[i] + current->z] && !touch[px[i] + current->x][py[i] + current->y][pz[i] + current->z]) {
q.push(point(px[i] + current->x, py[i] + current->y, pz[i] + current->z,current->layer+1));
touch[px[i] + current->x][py[i] + current->y][pz[i] + current->z] = 1;
}
}
q.pop();
}
if (layer > 0) {
printf("Escaped in %d minute(s).\n", layer);
}
else {
printf("Trapped!\n");
}
}
return 0;
}