ZOJ 1940 Dungeon Master bfs

来源:互联网 发布:华大基因 云计算 编辑:程序博客网 时间:2024/05/17 03:34
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!




       深度搜索   以前是4个方向,现在为6个方向 在此基础上做一下变通就可以



PS      打代码的时间一定要细心,一次性打正确,如果不正确返回来找是很难发现的 。。就是粗心写错一个字符 我花了两个多小时找错。。哎(细心为大 ,不能一味的追求打字的速度,。,。)

#include<cstdio>#include<cstring>#include<queue>#include<iostream>using namespace std;const int M=33;const int inf=0x3f3f3f3f;char s[M][M][M];int d[M][M][M];int z,x,y;int sx,sy,sz;int ex,ey,ez;int nx,ny,nz;struct point{    int l,c,r;};//利用结构体来储存指标值int dz[6]={-1,1,0,0,0,0},dy[6]={0,0,-1,1,0,0},dx[6]={0,0,0,0,-1,1};int bfs(){    queue<point> que;    point pp;    pp.l=sz,pp.c=sx,pp.r=sy;    que.push(pp);    d[sz][sx][sy]=0;    while(que.size())    {        point p=que.front();        que.pop();        if(p.l==ez&&p.c==ex&&p.r==ey)            break;        for(int i=0;i<6;i++)        {            nz=p.l+dz[i];            nx=p.c+dx[i];            ny=p.r+dy[i];            if(nx>=0&&nx<x&&ny>=0&&ny<y&&nz>=0&&nz<z&&s[nz][nx][ny]!='#'&&d[nz][nx][ny]==inf)            {                point np;                np.l=nz,np.c=nx,np.r=ny;                que.push(np);            d[nz][nx][ny]=d[p.l][p.c][p.r]+1;            }        }    }    return d[ez][ex][ey];}int main(){    int i,j,k;    while(scanf("%d%d%d",&z,&x,&y)&&(x+y+z))    {        memset(s,0,sizeof(s));        memset(d,inf,sizeof(d));        for(i=0;i<z;i++)        {            for(j=0;j<x;j++)            {                for( k=0;k<y;k++)                {                    cin>>s[i][j][k];                    if(s[i][j][k]=='S')                    {                        sx=j;sy=k;sz=i;                    }                    if(s[i][j][k]=='E')                    {                        ez=i;ex=j;ey=k;                    }                }            }        }        int res=bfs();        if(res==inf)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n",res);    }    return 0;}