水题:三维bfs
code:
#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
typedef struct{
int x,y,z,wal;
}data;
data star;
int n,k,ex,ey,ez,lev,hang,lie;
char maze[35][35][35];
int vis[35][35][35];
int dir[6][3]={ {1,0,0} , {0,1,0} , {0,0,1} , {0,-1,0} , {0,0,-1} , {-1,0,0} };
int bfs()
{
queue <data> q;
q.push(star);
while(q.size())
{
data now=q.front();
q.pop();
if(now.x==ex&&now.y==ey&&now.z==ez){
return now.wal;
}
data nex;
nex.wal=now.wal+1;
for(int i=0;i<6;i++)
{
int a=now.x+dir[i][0],b=now.y+dir[i][1],c=now.z+dir[i][2];
if(vis[a][b][c]==0)
if( 1 <= a && a <= lev && 1 <= b && b <= hang && 1 <= c && c <= lie )
if( maze[a][b][c] == '.' || maze[a][b][c] == 'E' )
{
vis[a][b][c]=1;
nex.x=a,nex.y=b,nex.z=c;
q.push(nex);
}
}
}
return 0;
}
int main(void)
{
while(cin>>lev>>hang>>lie&&lev+hang+lie)
{
memset(vis,0,sizeof vis);
for(int i=1;i<=lev;i++)
for(int j=1;j<=hang;j++)
scanf("%s",maze[i][j]+1);
for(int i=1;i<=lev;i++)
for(int j=1;j<=hang;j++)
for(int q=1;q<=lie;q++)
{
if( maze[i][j][q]=='S' ) {
star.x=i,star.y=j,star.z=q;
vis[i][j][q]=1;
star.wal=0;
}
if( maze[i][j][q]=='E' ) {
ex=i,ey=j,ez=q;
}
}
int ans=bfs();
if ( ans ) printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
}