宽搜模板题
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int l,r,c;
struct Node{
int s,x,y,step; //层数 行 列
Node(int a,int b,int c,int d){
s = a;
x = b;
y = c;
step = d;
}
};
char map[115][115][115];
int vis[115][115][115];
int dx[] = {1,-1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dt[] = {0,0,0,0,1,-1};
int bfs(int t,int x,int y)
{
queue< Node > q;
q.push(Node(t,x,y,0));
while(!q.empty())
{
Node now = q.front();
if(map[now.s][now.x][now.y] == 'E')
{
return now.step;
}
q.pop();
for(int i = 0;i < 6;i++)
{
int T = now.s+dt[i];
int X = now.x+dx[i];
int Y = now.y+dy[i];
int S = now.step+1;
if(T>=1&&T<=l&&X>=1&&X<=r&&Y>=1&&Y<=c&&!vis[T][X][Y]&&map[T][X][Y] != '#')
{
vis[T][X][Y] = 1;
q.push(Node(T,X,Y,S));
}
}
}
return 0;
}
int main(void)
{
while(cin>>l>>r>>c,l,r,c)
{
int x = 0,y =0,t = 0;
memset(vis,0,sizeof(vis));
for(int i = 1;i <= l;i++)
{
for(int j = 1;j <= r;j++)
{
for(int k = 1;k <= c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k] == 'S')
{
t = i;
x = j;
y = k;
}
}
}
}
vis[t][x][y] = 1;
int ans = 0;
ans = bfs(t,x,y);
if(ans)
{
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
else
cout<<"Trapped!"<<endl;
}
return 0;
}