bfs。
#include<iostream>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
const int dz[6] = {0, 0, 0, 0, 1, -1};
const int dx[6] = {0, 1, 0, -1, 0, 0};
const int dy[6] = {1, 0, -1, 0, 0, 0};
int L, R, C;
string m[35][35];
struct Node
{
int z, x, y, s;
Node(){}
Node(int z, int x, int y, int s)
{
this->z = z;
this->x = x;
this->y = y;
this->s = s;
}
};
Node start_u;
bool vis[35][35][35];
bool flag;
void bfs()
{
queue<Node> q;
q.push(start_u);
vis[start_u.z][start_u.x][start_u.y] = true;
Node u, v;
while(q.size())
{
u = q.front();
q.pop();
if(m[u.z][u.x][u.y] == 'E')
{
cout << "Escaped in " << u.s << " minute(s)." << endl;
flag = false;
return;
}
for(int i = 0; i < 6; i ++)
{
v.z = u.z + dz[i];
v.x = u.x + dx[i];
v.y = u.y + dy[i];
v.s = u.s + 1;
if(v.z < 0 || v.z >= L || v.x < 0 || v.x >= R || v.y < 0 || v.y >= C || m[v.z][v.x][v.y] == '#' || vis[v.z][v.x][v.y])
continue;
q.push(v);
vis[v.z][v.x][v.y] = true;
}
}
}
int main()
{
while(cin >> L >> R >> C && !(!L && !R && !C))
{
memset(vis, false, sizeof(vis));
flag = true;
for(int i = 0; i < L; i ++)
for(int j = 0; j < R; j ++)
cin >> m[i][j];
for(int i = 0; i < L; i ++)
for(int j = 0; j < R; j ++)
for(int k = 0; k < C; k ++)
if(m[i][j][k] == 'S')
start_u = Node(i, j, k, 0);
bfs();
if(flag)
cout << "Trapped!" << endl;
}
// system("pause");
return 0;
}