广搜入门题,不过图是三维的~~
AC代码:
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define N 35
#include<cstdio>
using namespace std;
typedef struct node
{
int x;
int y;
int z;
int step;
friend bool operator<(node a,node b)
{return a.step>b.step;}
}Node;
Node now,cur;
int endx,endy,endz,L,m,n;
char map[N][N][N];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
int bfs()
{
priority_queue<Node>Q;
Q.push(now);
map[now.x][now.y][now.z]='#';
while(!Q.empty())
{
cur=Q.top();
Q.pop();
int xx=cur.x;
int yy=cur.y;
int zz=cur.z;
int step=cur.step;
if(xx==endx&&yy==endy&&zz==endz) return step;
for(int i=0;i!=6;++i)
{
int a=xx+dx[i];
int b=yy+dy[i];
int c=zz+dz[i];
if(a>=0&&a<L&&b>=0&&b<m&&c>=0&&c<n&&map[a][b][c]!='#')
{
now.x=a,now.y=b,now.z=c,now.step=step+1;
Q.push(now);
map[a][b][c]='#';
}
}
}return -1;
}
int main()
{
while(~scanf("%d%d%d",&L,&m,&n))
{
if(!L&&!m&&!n) break;
for(int i=0;i<L;++i)
for(int j=0;j<m;++j)
{
scanf("%s",map[i][j]);
for(int k=0;k<n;++k)
{
if(map[i][j][k]=='S') now.x=i,now.y=j,now.z=k,now.step=0;
else if(map[i][j][k]=='E') endx=i,endy=j,endz=k;
}
}
int ans=bfs();
if(ans==-1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",ans);
}return 0;
}