立体bfs,共有六个方向:
const int dx[] = {0,0,1,-1,0,0};
const int dy[] = {1,-1,0,0,0,0};
const int dz[] = {0,0,0,0,1,-1};
#include
#include
#include
using namespace std;
const int maxn = 32;
char G[maxn][maxn][maxn];
int d[maxn][maxn][maxn];
const int dx[] = {0,0,1,-1,0,0};
const int dy[] = {1,-1,0,0,0,0};
const int dz[] = {0,0,0,0,1,-1};
int l, r, c;
struct node{
int x, y, z;
node(){
}
node(int x, int y, int z):x(x), y(y), z(z){
}
};
int bfs(int z, int x, int y) {
memset(d, -1, sizeof(d));
queueq;
q.push(node(x, y, z));
d[z][x][y] = 0;
while(!q.empty()){
node p =q.front();
q.pop();
x = p.x, y = p.y, z = p.z;
if(G[z][x][y] == 'E') return d[z][x][y];
for(int i = 0; i < 6; ++i){
int px = x + dx[i], py = y + dy[i], pz = z + dz[i];
if(px < 0 || py < 0 || pz < 0 || px >= r || py >= c || pz >= l) continue;
if(G[pz][px][py] == '#' || d[pz][px][py] != -1) continue;
d[pz][px][py] = d[z][x][y] + 1;
q.push(node(px, py, pz));
}
}
return -1;
}
int main(){
while(scanf("%d%d%d", &l, &r, &c) == 3 && l){
for(int i = 0; i < l; ++i){
for(int j = 0; j < r; ++j) scanf("%s", G[i][j]);
}
int ans;
int x, y, z;
for(int i = 0; i < l; ++i)
for(int j = 0; j < r; ++j)
for(int k = 0; k < c; ++k) {
if(G[i][j][k] == 'S') {
z = i, x = j, y = k;
i = l, j = r, k = c;
}
}
ans = bfs(z, x, y);
if(ans == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}