poj2251
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
struct node
{
int x, y, z;
int step;
};
int L, R, C;
char a[35][35][35];
int vis[35][35][35];
int dir[10][5] = { {1,0,0},{-1,0,0}, {0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
queue<node> q;
int bfs(node s, node e)
{
memset(vis, 0, sizeof(vis));
while (!q.empty()) q.pop();
s.step = 0;
vis[s.x][s.y][s.z] = 1;
q.push(s);
while (!q.empty())
{
node now = q.front();
q.pop();
if (now.x == e.x && now.y == e.y && now.z == e.z)
return now.step;
for (int i = 0; i < 6; i++)
{
node t;
t.x = now.x + dir[i][0];
t.y = now.y + dir[i][1];
t.z = now.z + dir[i][2];
if (t.x >= 1 && t.x <= L &&
t.y >= 1 && t.y <= R &&
t.z >= 1 && t.z <= C &&
a[t.x][t.y][t.z] != '#' &&
!vis[t.x][t.y][t.z])
{
vis[t.x][t.y][t.z] = 1;
t.step = now.step + 1;
q.push(t);
}
}
}
return -1;
}
int main()
{
while (1)
{
memset(a, 0,sizeof(a));
memset(vis, 0, sizeof(vis));
cin >> L >> R >> C;
if (L == 0 && R == 0 && C == 0)
break;
for (int i = 1; i <= L; i++)
for (int j = 1; j <= R; j++)
for (int k = 1; k <= C; k++)
cin >> a[i][j][k];
node s, e;
for (int i = 1; i <= L; i++)
for (int j = 1; j <= R; j++)
for (int k = 1; k <= C; k++)
{
if (a[i][j][k] == 'S')
{
s.x = i; s.y = j; s.z = k;
}
if (a[i][j][k] == 'E')
{
e.x = i; e.y = j; e.z = k;
}
}
int ans = bfs(s, e);
if (ans == -1)
cout << "Trapped!" << endl;
else
cout << "Escaped in " << ans << " minute(s)." << endl;
}
return 0;
}