就是很简单很标准的BFS
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w, h, l;
int sx, sy, sl,el,ex,ey;
bool a[31][31][31];
int ans;
int dir[3][6] = { 0, 0, 0, 0, -1, 1,
-1, 1, 0, 0, 0, 0,
0, 0, -1, 1, 0, 0 };
struct node{
int l, x, y, lenth;
};
void bfs(int z, int x, int y){
node s[30000];
bool visit[31][31][31] = { false };
int head = 0, tail = 0;
//printf("z=%d x=%d y=%d\n", z, x, y);
s[head].x = x;
s[head].y = y;
s[head].l = z;
s[head].lenth = 0;
tail++;
visit[l][x][y] = true;
while (head < tail){
int nl, nx, ny;
for (int i = 0; i < 6; i++){
nl = s[head].l + dir[0][i];
nx = s[head].x + dir[1][i];
ny = s[head].y + dir[2][i];
if (nl < 0 || nl >= l || nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
if (visit[nl][nx][ny] == false && a[nl][nx][ny] == true){
//printf("nl=%d nx=%d ny=%d\n", nl, nx, ny);
s[tail].l = nl;
s[tail].x = nx;
s[tail].y = ny;
s[tail].lenth = s[head].lenth + 1;
if (nl == el&&nx == ex&&ny == ey){
ans = ans < s[tail].lenth ? ans : s[tail].lenth;
return;
}
tail++;
visit[nl][nx][ny] = true;
}
}
//visit[s[head].l][s[head].x][s[head].y] = true;
head++;
}
}
int main(){
while (1){
//scanf("%d%d%d", &l, &h, &w);
cin >> l >> h >> w;
if (l == 0)break;
char c;
memset(a, true, sizeof(a));
//memset(visit, false, sizeof(visit));
for (int i = 0; i < l; i++){
for (int j = 0; j < h; j++){
for (int k = 0; k < w; k++){
//scanf("%c", &c);
cin >> c;
if (c == 'S'){
sl = i, sx = j, sy = k;
}
else if (c == '#'){
a[i][j][k] = false;
}
else if (c == 'E'){
el = i, ex = j, ey = k;
}
}
}
}
/*for (int i = 0; i < l; i++){
for (int j = 0; j < h; j++){
for (int k = 0; k < w; k++){
if (a[i][j][k])printf(".");
else printf("#");
}
printf("\n");
}
printf("\n");
}*/
ans = 30000;
bfs(sl, sx, sy);
if (ans < 30000)printf("Escaped in %d minute(s).\n", ans);
else printf("Trapped!\n");
}
}