Summer Camp (1) Third bomb-----Dungeon Master (Poj2251)

Source: Internet
Author: User

Description

You is trapped in a 3D dungeon and need to find the quickest-on-the-do out! The dungeon is composed of a unit cubes which may or may isn't being 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 are surrounded by solid rock in all sides.

is an escape possible? If Yes, how long would it take?

Input

The input consists of a number of dungeons. Each of the dungeon description starts with a line containing three integers L, R and C (all limited to the size).
L is the number of levels making up the dungeon.
R and C is the number of rows and columns making up the plan of each level.
Then there'll follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock was indicated by a ' # ' and empty cells were 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 isn't possible to escape, print the line
trapped!

Sample Input

3 4 5s.....###. ##.. ###.#############.####...###########.###### #E1 3 3s## #E # # #0 0 0

Sample Output

Escaped in minute (s). trapped!





Problem analysis: Still simple BFS, but to use a three-dimensional array to store the map, but there are 6 branches, using an array to store the direction can make the program more concise.

1#include"iostream"2#include"Queue"3 using namespacestd;4 Charmaze[ +][ +][ +];5 intv[6][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0};6 intl,r,c;7 structEscaper8 {9     inti;Ten     intJ; One     intK; A     intTime ; - }; - Escaper fir; the voidMbegin () - { -     for(intI=0; i<=l+1; i++) -      for(intj=0; j<=r+1; j + +) +       for(intk=0; k<=c+1; k++) -         if(I*j*k = =0|| i = = L +1|| j==r+1|| k==c+1) +MAZE[I][J][K] ='#'; A        Else at        { -Cin>>Maze[i][j][k]; -          if(Maze[i][j][k] = ='S') -               { -FIR.I =i; -FIR.J =J; inFIR.K =K; -               } to         } + } - voidBFS () the { *Queue <escaper>p; $ escaper sec;Panax NotoginsengFir.time=0; - P.push (FIR); the       while(!p.empty ()) +    { ASEC =P.front (); the P.pop (); +       for(intI=0;i<6; i++) -       { $FIR.I = sec.i+v[i][0]; $FIR.J = sec.j+v[i][1]; -FIR.K = sec.k+v[i][2]; -         if(MAZE[FIR.I][FIR.J][FIR.K]! ='#') the           { -Fir.time = sec.time+1;Wuyi               if(MAZE[FIR.I][FIR.J][FIR.K] = ='E') the                    { -cout<<"escaped in"<<fir.time<<"minute (s)."<<Endl; Wu                        return; -                    } AboutMAZE[FIR.I][FIR.J][FIR.K] ='#'; $ P.push (FIR); -           } -       } -    } Acout<<"trapped!"<<Endl; + } the intMain () - { $      while(cin>>l>>r>>c && L && R &&C) the     { the Mbegin (); the BFS (); the     } -    return 0; in}
View Code


Summer Camp (1) Third bomb-----Dungeon Master (Poj2251)

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.