#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;
char maze[31][31][31];
const int pmove[4][2]={{0,1},{0,-1},{-1,0},{1,0}};//移动
int z;//多少组
int n;//多少行
int m;//多少列
int sx,sy,ex,ey,sz,ez;
int Dungeon();
struct Node
{
friend bool operator < (Node a,Node b)
{
return a.step>b.step;
}
int x,y,z,step;
};
int main()
{
int i,j;
while(cin>>z>>n>>m)
{
memset(maze,'#',sizeof(maze));
if(z==0&&n==0&&m==0)
break;
for(int icase=0;icase<z;icase++)
{
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>maze[icase][i][j];
if(maze[icase][i][j]=='S')
{
sy=i;
sx=j;
sz=icase;
continue;
}
if(maze[icase][i][j]=='E')
{
ey=i;
ex=j;
ez=icase;
}
}
}
int tmp;
tmp=Dungeon();
if(tmp)
cout<<"Escaped in "<<tmp<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}
int Dungeon()
{
priority_queue<Node> que;
Node now,temp;
int v;
now.x=sx;
now.y=sy;
now.z=sz;
now.step=0;
que.push(now);
maze[sz][sy][sx]='#';
while(!que.empty())
{
temp=que.top();
que.pop();
if(temp.x==ex&&temp.y==ey&&temp.z==ez)
return temp.step;
for(int i=0;i<z;i++)//穿越
{
if(temp.z-1>=0)
if(maze[temp.z-1][temp.y][temp.x]!='#')
{
now=temp;
now.z--;
now.step++;
que.push(now);
maze[temp.z][temp.y][temp.x]='#';
maze[temp.z-1][temp.y][temp.x]='#';
}
if(temp.z+1<z)
if(maze[temp.z+1][temp.y][temp.x]!='#')
{
now=temp;
now.z++;
now.step++;
que.push(now);
maze[temp.z+1][temp.y][temp.x]='#';
}
}
for(v=0;v<4;v++)// 正常移动
{
for(int i=0;i<z;i++)//穿越
{
if(temp.z-1>=0)
if(maze[temp.z-1][temp.y][temp.x]=='.')
{
now=temp;
now.z--;
now.step++;
que.push(now);
maze[temp.z][temp.y][temp.x]='#';
maze[temp.z-1][temp.y][temp.x]='#';
}
if(temp.z+1<z)
if(maze[temp.z+1][temp.y][temp.x]=='.')
{
now=temp;
now.z++;
now.step++;
que.push(now);
maze[temp.z+1][temp.y][temp.x]='#';
}
}
now.x=temp.x+pmove[v][0];
now.y=temp.y+pmove[v][1];
now.z=temp.z;
now.step=temp.step+1;
if(now.x==ex&&now.y==ey&&temp.z==ez)
return now.step;
if(now.x<0||now.y<0||now.x>m||now.y>n)
continue;
if(maze[temp.z][now.y][now.x]!='#')
{
que.push(now);
maze[temp.z][now.y][now.x]='#';
}
}
}
return 0;
}
zoj1940-Dungeon Master
最新推荐文章于 2019-10-16 18:15:26 发布