三维的迷宫问题,常规BFS即可,代码长但写起来不难。1A。
#include <cmath>
#include<queue>
#include <cstring>
#include <cstdio>
#include <map>
#include <iostream>
#include <vector>
#include<cmath>
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
char Map[35][35][35];
int vis[35][35][35];
struct status
{
int i,j,k,t;
};
int main()
{
int l,r,c;
while(cin>>l>>r>>c)
{
if(!l)break;
memset(vis,0,sizeof(vis));
status start,End;
rep(i,1,l)
{
rep(j,1,r)
{
rep(k,1,c)
{
cin>>Map[i][j][k];
if(Map[i][j][k]=='S')
{
start.i=i;
start.j=j;
start.k=k;
start.t=0;
}
if(Map[i][j][k]=='E')
{
End.i=i;
End.j=j;
End.k=k;
End.t=-1;
}
}
}
}
queue< status >que;
que.push(start);
vis[start.i][start.j][start.k]=1;
int ans=-1;
while(!que.empty())
{
//cout<<1<<endl;
status now=que.front();
que.pop();
if(now.i==End.i&&now.j==End.j&&now.k==End.k)
{
ans=now.t;
break;
//cout<<1;
}
if(now.i-1>=1&&now.i-1<=l&&(Map[now.i-1][now.j][now.k]=='.'||Map[now.i-1][now.j][now.k]=='E')&&!vis[now.i-1][now.j][now.k])
{
status t;
t.i=now.i-1;
t.j=now.j;
t.k=now.k;
t.t=now.t+1;
que.push(t);
vis[now.i-1][now.j][now.k]=1;
}
if(now.j-1>=1&&now.j-1<=r&&(Map[now.i][now.j-1][now.k]=='.'||Map[now.i][now.j-1][now.k]=='E')&&!vis[now.i][now.j-1][now.k])
{
status t;
t.i=now.i;
t.j=now.j-1;
t.k=now.k;
t.t=now.t+1;
que.push(t);
vis[now.i][now.j-1][now.k]=1;
}
if(now.k-1>=1&&now.k-1<=c&&(Map[now.i][now.j][now.k-1]=='.'||Map[now.i][now.j][now.k-1]=='E')&&!vis[now.i][now.j][now.k-1])
{
status t;
t.i=now.i;
t.j=now.j;
t.k=now.k-1;
t.t=now.t+1;
que.push(t);
vis[now.i][now.j][now.k-1]=1;
}
if(now.k+1>=1&&now.k+1<=c&&(Map[now.i][now.j][now.k+1]=='.'||Map[now.i][now.j][now.k+1]=='E')&&!vis[now.i][now.j][now.k+1])
{
//cout<<1;
status t;
t.i=now.i;
t.j=now.j;
t.k=now.k+1;
t.t=now.t+1;
que.push(t);
vis[now.i][now.j][now.k+1]=1;
}
if(now.j+1>=1&&now.j+1<=r&&(Map[now.i][now.j+1][now.k]=='.'||Map[now.i][now.j+1][now.k]=='E')&&!vis[now.i][now.j+1][now.k])
{
status t;
t.i=now.i;
t.j=now.j+1;
t.k=now.k;
t.t=now.t+1;
que.push(t);
vis[now.i][now.j+1][now.k]=1;
}
if(now.i+1>=1&&now.i+1<=l&&(Map[now.i+1][now.j][now.k]=='.'||Map[now.i+1][now.j][now.k]=='E')&&!vis[now.i+1][now.j][now.k])
{
status t;
t.i=now.i+1;
t.j=now.j;
t.k=now.k;
t.t=now.t+1;
que.push(t);
vis[now.i+1][now.j][now.k]=1;
}
}
if(ans>0)
{
printf("Escaped in %d minute(s).\n",ans);
}
else
cout<<"Trapped!"<<endl;
}
}