poj2251 Dungeon Master

来源:互联网 发布:http load windows 64 编辑:程序博客网 时间:2024/06/01 22:10

Dungeon Master

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 5

S....

.###.

.##..

###.#

 

#####

#####

##.##

##...

 

#####

#####

#.###

####E

 

1 3 3

S##

#E#

###

 

0 0 0

Sample Output

Escaped in 11 minute(s).

Trapped!

 

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<iostream>

#include<queue>

using namespace std;

char c[33][33][33];

int mark[33][33][33];//标记这个点用没用过

int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}}; //6个方向

int x1,y1,z1,x2,y2,z2;

int n,m,w;

struct node

{

    int x,y,z;

    int time;

};

int go(int x,int y,int z)

{

    if(0<=x&&x<n&&0<=y&&y<m&&0<=z&&z<w&&c[x][y][z]!='#')//可以前进

        return 1;

    return 0;

}

int bfs()

{

    int i;

    node st,ed;

    queue<node>que;

    memset(mark,0,sizeof(mark));

    st.x=x1;

    st.y=y1;

    st.z=z1;

    st.time=0;

    mark[st.x][st.y][st.z]=1;

    que.push(st);

    while(!que.empty())

    {

        st=que.front();

        que.pop();

        if(st.x==x2&&st.y==y2&&st.z==z2)

            return st.time;

        for(i=0;i<6;i++)

        {

            ed.x=st.x+dir[i][0];

            ed.y=st.y+dir[i][1];

            ed.z=st.z+dir[i][2];

            if(go(ed.x,ed.y,ed.z)&&!mark[ed.x][ed.y][ed.z])

            {

                mark[ed.x][ed.y][ed.z]=1;

                ed.time=st.time+1;

                que.push(ed);

            }

        }

    }

    return 0;

}

int main()

{

    while(~scanf("%d%d%d",&n,&m,&w))

    {

        if(n==0&&m==0&&w==0)

            break;

        for(int i=0;i<n;i++)

        {

            for(int j=0;j<m;j++)

            {

                scanf("%s",c[i][j]);

                for(int k=0;k<w;k++)

                {

                    if(c[i][j][k]=='S')

                    {

                        x1=i;y1=j;z1=k;

                    }

                    if(c[i][j][k]=='E')

                    {

                        x2=i;y2=j;z2=k;

                    }

                }

            }

        }

        int ans;

        ans=bfs();

        if(ans)

            printf("Escaped in %d minute(s).\n",ans);

        else

            printf("Trapped!\n");

    }

    return 0;

}

 

 

题意:一个三维的地牢,给你起点和终点,问从起点到终点至少需要多长时间,每移动一次耗时1minute;如果不能到达终点,输出“Trapped。因为是三维的,所以从 每个点开始搜索时都会有6个方向,设置6个方向向量,然后每搜到一个点就判断是不是终点。利用队列即可。

原创粉丝点击