本题就是三维的dfs,第一次接触有点不熟练,之后就慢慢好了,另外注意在dfs中那些变量和结构体尽量声明新的,防止出错;还有一点就是,记住有6种情况要依次遍历
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=50;
char a[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int c[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node{
int x,y,z;
int cnt;
};
int n,m,h,cnt;
int x1,y1,z1;
void dfs()
{
queue<node> q;
node r;
r.x=x1,r.y=y1,r.z=z1,r.cnt=0;
vis[x1][y1][z1]=1;
q.push(r);
while(!q.empty())
{
node res=q.front();q.pop();
int xx=res.x,yy=res.y,zz=res.z;
cnt=res.cnt;
//cout<<xx<<yy<<zz<<endl;
if(a[xx][yy][zz]=='E'){
printf("Escaped in %d minute(s).\n",cnt);return;
}
for(int i=0;i<6;i++)
{
node rhs;
int xi=rhs.x=c[i][0]+xx;
int yi=rhs.y=c[i][1]+yy;
int zi=rhs.z=c[i][2]+zz;
rhs.cnt=cnt+1;
if(xi>n||xi<1||yi>m||yi<1||zi>h||zi<1) continue;
if(a[xi][yi][zi]!='#'&&!vis[xi][yi][zi])
{
vis[xi][yi][zi]=1;
q.push(rhs);
}
}
}
printf("Trapped!\n");
}
int main()
{
//freopen("B.txt","r",stdin);
while(scanf("%d %d %d",&n,&m,&h)!=EOF)
{
// cout<<n<<m<<h<<endl;
memset(vis,0,sizeof(vis));
if(n==0&&m==0&&h==0) break;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
for(int z=1;z<=h;z++)
{
cin>>a[i][j][z];
//cout<<a[i][j][z]<<endl;
if(a[i][j][z]=='S') x1=i,y1=j,z1=z;
}
dfs();
//cout<<cnt<<endl;
}
return 0;
}