UVA 532 Dungeon Master

UVA_532

    这个题目在一般图的遍历的基础上增加了一维,多了两个可以选择的方向。

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

  

你可能感兴趣的:(master)