poj 2251 Dungeon Master

#include<iostream>        //bfs
using namespace std;
int vis[30][30][30],pos[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
char arr[30][30][30];
int l,r,c;
struct node
{
int d,x,y,p;
}ans[
30000];
int main()
{
int sd,sx,sy;
while(cin>>l>>r>>c&&l)
{
memset(vis,
0,sizeof(vis));
for(int i=0;i<l;++i)
for(int j=0;j<r;++j)
for(int k=0;k<c;++k)
{
cin
>>arr[i][j][k];
if(arr[i][j][k]=='S')
sd
=i,sx=j,sy=k,vis[i][j][k]=1;
}
int head=0,t=1,tag=1;
ans[
0].d=sd;ans[0].x=sx;ans[0].y=sy;ans[0].p=0;
while(head<t&&tag)
{
node temp
=ans[head++];
for(int i=0;i<4&&tag;++i)
{
int tx=temp.x+pos[i][0],ty=temp.y+pos[i][1];
if(tx>=0&&tx<r&&ty>=0&&ty<c)
{
if(arr[temp.d][tx][ty]=='E')
{
printf(
"Escaped in %d minute(s).\n",temp.p+1);
tag
=0;
}
else if(arr[temp.d][tx][ty]=='.'&&!vis[temp.d][tx][ty])
{
vis[temp.d][tx][ty]
=1;
ans[t].p
=temp.p+1;ans[t].d=temp.d;ans[t].x=tx;ans[t].y=ty;
t
++;
}
}
}
if(tag&&l>temp.d+1&&arr[temp.d+1][temp.x][temp.y]!='#'&&!vis[temp.d+1][temp.x][temp.y])
{
if(arr[temp.d+1][temp.x][temp.y]=='E')
{
printf(
"Escaped in %d minute(s).\n",temp.p+1);
tag
=0;
}
else
{
ans[t].p
=temp.p+1;ans[t].d=temp.d+1;ans[t].x=temp.x;ans[t].y=temp.y;
t
++;
vis[temp.d
+1][temp.x][temp.y]=1;
}
}
if(tag&&temp.d>0&&arr[temp.d-1][temp.x][temp.y]!='#'&&!vis[temp.d-1][temp.x][temp.y])
{
if(arr[temp.d-1][temp.x][temp.y]=='E')
{
printf(
"Escaped in %d minute(s).\n",temp.p+1);
tag
=0;
}
else
{
ans[t].p
=temp.p+1;ans[t].d=temp.d-1;ans[t].x=temp.x;ans[t].y=temp.y;
t
++;
vis[temp.d
-1][temp.x][temp.y]=1;
}
}
}
if(tag)
printf(
"Trapped!\n");
}
return 0;
}

  

posted on 2011-07-22 20:12  sysu_mjc  阅读(116)  评论(0编辑  收藏  举报

导航