POJ 2251 Dungeon Master

POJ_2251

    这个题目相比之前做过的BFS遍历迷宫的问题,相当于增加了一维的空间,这样在移动的时候就多了两种选择。

#include<stdio.h>
#include
<string.h>
int a[35][35][35],dis[35][35][35],vis[35][35][35];
int qx[27010],qy[27010],ql[27010];
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
char b[50];
int main()
{
int i,j,k,L,R,C,front,rear,sx,sy,sl,tx,ty,tl,l,x,y,newl,newx,newy;
while(1)
{
scanf(
"%d%d%d",&L,&R,&C);
if(L==0)
break;
memset(a,
-1,sizeof(a));
for(i=1;i<=L;i++)
for(j=1;j<=R;j++)
{
scanf(
"%s",b+1);
for(k=1;k<=C;k++)
{
if(b[k]=='.')
a[i][j][k]
=0;
else if(b[k]=='S')
{
a[i][j][k]
=1;
sl
=i;
sx
=j;
sy
=k;
}
else if(b[k]=='E')
{
a[i][j][k]
=2;
tl
=i;
tx
=j;
ty
=k;
}
}
}
memset(vis,
0,sizeof(vis));
front
=rear=0;
ql[rear]
=sl;
qx[rear]
=sx;
qy[rear]
=sy;
rear
++;
dis[sl][sx][sy]
=0;
dis[tl][tx][ty]
=-1;
while(front<rear)
{
l
=ql[front];
x
=qx[front];
y
=qy[front];
front
++;
if(a[l][x][y]==2)
break;
for(i=0;i<4;i++)
{
newx
=x+dx[i];
newy
=y+dy[i];
if(!vis[l][newx][newy]&&a[l][newx][newy]>-1)
{
vis[l][newx][newy]
=1;
dis[l][newx][newy]
=dis[l][x][y]+1;
ql[rear]
=l;
qx[rear]
=newx;
qy[rear]
=newy;
rear
++;
}
}
for(i=-1;i<=1;i+=2)
{
newl
=l+i;
if(!vis[newl][x][y]&&a[newl][x][y]>-1)
{
vis[newl][x][y]
=1;
dis[newl][x][y]
=dis[l][x][y]+1;
ql[rear]
=newl;
qx[rear]
=x;
qy[rear]
=y;
rear
++;
}
}
}
if(dis[tl][tx][ty]<0)
printf(
"Trapped!\n");
else
printf(
"Escaped in %d minute(s).\n",dis[tl][tx][ty]);
}
return 0;
}

  

你可能感兴趣的:(master)