Dungeon Master poj 2251 DFS

Source: Internet
Author: User

Language: Default Dungeon Master
Time limit:1000 ms   Memory limit:65536 K
Total submissions:16855   Accepted:6564

Description

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 '. '. 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!

Source

Ulm Local 1997

Question: give you a three-dimensional maze to calculate the shortest steps from the start point to the end point.

Idea: the problem is similar to the two-dimensional maze. Just change it to three-dimensional.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <queue>using namespace std;struct Node{    int x,y,z;    int step;};char mp[35][35][35];int visit[35][35][35];int dir[6][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,1,0,0,-1};//x,y,zint N,M,P,sx,sy,sz,ex,ey,ez;bool ISok(int x,int y,int z){    if (z>=0&&z<P&&x>=0&&x<N&&y>=0&&y<M&&mp[z][x][y]!='#'&&!visit[z][x][y])        return true;    return false;}void bfs(){    Node st,now;    memset(visit,0,sizeof(visit));    queue<Node>Q;    visit[sz][sx][sy]=1;    st.x=sx;st.y=sy;st.z=sz;    st.step=0;    Q.push(st);    while (!Q.empty())    {        st=Q.front();        Q.pop();        if (st.x==ex&&st.y==ey&&st.z==ez)        {            printf("Escaped in %d minute(s).\n",st.step);            return ;        }        for (int i=0;i<6;i++)        {            now.x=st.x+dir[i][0];            now.y=st.y+dir[i][1];            now.z=st.z+dir[i][2];            if (ISok(now.x,now.y,now.z))            {                now.step=st.step+1;                visit[now.z][now.x][now.y]=1;                Q.push(now);            }        }    }    printf("Trapped!\n");    return ;}int main(){    while (scanf("%d%d%d",&P,&N,&M)&&P)    {        for (int i=0;i<P;i++)        {            for (int j=0;j<N;j++)            {                scanf("%s",mp[i][j]);//z,x,y                for (int t=0;t<M;t++)                {                    if (mp[i][j][t]=='S')                    {                        sx=j;sy=t;sz=i;                    }                    if (mp[i][j][t]=='E')                    {                        ex=j;ey=t;ez=i;                    }                }            }        }        bfs();    }    return 0;}




Dungeon Master poj 2251 DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.