三维空间的 BFS ,同理搞之。。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
#define MAX 1000009
#define INF 0x3f3f3f3f
#define MS(x) memset(x,0,sizeof(x))
#define ll long long
//#define P pair<int,int>
#define fst first
#define sec second
int dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
char b[40][40][40];
int dist[40][40][40];
bool used[40][40][40];
struct P
{
int x,y,z;
};
P S,E;
int X,Y,Z;
int bfs()
{
for(int i=0;i<40;i++)
for(int j=0;j<40;j++)
for(int k=0;k<40;k++)
dist[i][j][k]=INF;
MS(used);
queue<P> q;
q.push(S);
dist[S.z][S.x][S.y]=0;
used[S.z][S.x][S.y]=1;
while(!q.empty())
{
P tp=q.front();q.pop();
for(int i=0;i<6;i++)
{
int tx=tp.x+dir[i][0],ty=tp.y+dir[i][1],tz=tp.z+dir[i][2];
if(tx>=0&&tx<X&&ty>=0&&ty<Y&&tz>=0&&tz<Z&&b[tz][tx][ty]!='#'&&!used[tz][tx][ty])
{
used[tz][tx][ty]=1;
dist[tz][tx][ty]=dist[tp.z][tp.x][tp.y]+1;
q.push((P){tx,ty,tz});
}
}
}
return dist[E.z][E.x][E.y];
}
int main()
{
freopen("acm.in","r",stdin);
while(scanf("%d%d%d",&Z,&X,&Y)&&X!=0)
{
for(int z=0;z<Z;z++)
{
for(int x=0;x<X;x++)
{
scanf("%s",b[z][x]);
for(int y=0;y<Y;y++)
{
if(b[z][x][y]=='S')
S.x=x,S.y=y,S.z=z;
if(b[z][x][y]=='E')
E.x=x,E.y=y,E.z=z;
}
}
}
int ans=bfs();
if(ans==INF)
cout<<"Trapped!"<<endl;
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}