BFS+三维数组(参考二维走迷宫问题)
#include<iostream>
#include<map>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> PII;
typedef pair<int,PII> M;
const int N=1e2+10;
char g[N][N][N];
int L,R,C;
int x,y,z;
int d[N][N][N];
int i,j,l;
int bfs()
{
memset(d,-1,sizeof d);
queue<M> q;
q.push({x,{y,z}});
d[x][y][z] = 0;
int dx[] = {1,-1,0,0,0,0},dy[] = {0,0,1,-1,0,0},dz[] ={0,0,0,0,1,-1};
while(q.size()){
auto t = q.front();
q.pop();
int a = t.first,b = t.second.first,c = t.second.second;
for(int u=0;u<6;u++){
int ux= a + dx[u], uy = b + dy[u], uz = c + dz[u];
if(ux>=0&&ux<L&&uy>=0&&uy<R&&uz>=0&&uz<C&&d[ux][uy][uz]==-1&&(g[ux][uy][uz]=='.'||g[ux][uy][uz]=='E')){
d[ux][uy][uz] = d[a][b][c] + 1;
q.push({ux,{uy,uz}});
}
}
}
// cout<<x<<" "<<y<<" "<<z<<endl;
return d[i][j][l];
}
int main()
{
while(cin>>L>>R>>C){
if(L==0||R==0||C==0) return 0;
for(int u=0;u<L;u++){
for(int h=0;h<R;h++){
for(int k=0;k<C;k++){
cin>>g[u][h][k];
if(g[u][h][k]=='S') {
x=u,y=h,z=k;
}
if(g[u][h][k]=='E') {
i=u,j=h,l=k;
}
}
}
}
if(bfs()!=-1){
cout<<"Escaped in "<<bfs()<<" minute(s)."<<endl;
}
else{
cout<<"Trapped!"<<endl;
}
}
}