Description
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
Sample Output
Trapped!
广搜
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
const int maxn=35;
int b[maxn][maxn][maxn],f[maxn][maxn][maxn],h,n,m;
struct point
{
int x,y,z,cnt;
point(int x=0,int y=0,int z=0,int cnt=0):x(x),y(y),z(z),cnt(cnt){};
bool operator ==(const point&a)
{
if (x==a.x&&y==a.y&&z==a.z) return true;
return false;
}
}bg,ed,c[6];
point operator+(const point&a,const point&b)
{
return point(a.x+b.x,a.y+b.y,a.z+b.z,a.cnt+b.cnt);
}
bool check(const point& a)
{
if (a.x<1||a.x>h) return false;
if (a.y<1||a.y>n) return false;
if (a.z<1||a.z>m) return false;
if (b[a.x][a.y][a.z]) return false;
if (f[a.x][a.y][a.z]>=0) return false;
f[a.x][a.y][a.z]=a.cnt;
return true;
}
string s;
int bfs(point &x)
{
int ans=0;
memset(f,-1,sizeof(f));
f[x.x][x.y][x.z]=0;
queue<point> p;
p.push(x);
while (!p.empty())
{
point q=p.front(); p.pop();
if (q==ed) {ans=q.cnt; break;}
for (int i=0;i<6;i++)
if (check(q+c[i])) p.push(q+c[i]);
}
return ans;
}
int main()
{
c[0]=point(1,0,0,1);
c[1]=point(-1,0,0,1);
c[2]=point(0,1,0,1);
c[3]=point(0,-1,0,1);
c[4]=point(0,0,1,1);
c[5]=point(0,0,-1,1);
while (cin>>h>>n>>m,h+n+m)
{
for (int i=1;i<=h;i++)
for (int j=1;j<=n;j++)
{
cin>>s;
for (int k=1;k<=m;k++)
{
if (s[k-1]=='#') b[i][j][k]=1;
else b[i][j][k]=0;
if (s[k-1]=='S') bg=point(i,j,k);
if (s[k-1]=='E') ed=point(i,j,k);
}
}
int ans=bfs(bg);
if (ans>0) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}