POJ 2251-dungeon Master (three-dimensional space for shortest path)

Source: Internet
Author: User

DescriptionYou 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?InputThe 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.outputeach 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!

Open three-dimensional array with BFS.
1#include <cstdio>2#include <queue>3 using namespacestd;4 intdx[6]={-1,1,0,0,0,0};5 intdy[6]={0,0,-1,1,0,0};6 intdz[6]={0,0,0,0,1,-1};7 Charstr[ -][ -][ -];8 intL,r,c,i,j,k,ans,bx,bz,by;9 structStuTen { One     intX,y,z,step; A }; - BOOLF (Stu St) - { the     if(st.x<0|| st.z<0|| st.y<0|| St.x>=r | | St.y>=c | | St.z>=l | | STR[ST.Z][ST.X][ST.Y] = ='#') -     { -         return false; -     } +     return true; - } + intBFsintBzintBxintby ) A { atstr[bz][bx][by]='#'; -     intNx,ny,time,zz,xx,yy; -Queue<stu>que; - Stu St,next; -st.x=BX; -st.y=by ; inst.z=BZ; -st.step=0; to Que.push (ST); +  -      while(!que.empty ()) the     {     *      $st=Que.front ();Panax Notoginseng Que.pop (); -          thexx=St.x; +yy=St.y; Azz=st.z; theTime=St.step; +          for(i =0; I <6; i++) -         { $st.x=xx+Dx[i]; $st.y=yy+Dy[i]; -st.z=zz+Dz[i]; -             if(!F (ST)) the             { -                 Continue;Wuyi             } the             if(Str[st.z][st.x][st.y] = ='E') -             { Wu                 returntime+1; -             } About  $st.step=time+1; - Que.push (ST); -str[st.z][st.x][st.y]='#'; -         } A     } +     return 0; the } - intMain () $ { the  while(SCANF (" %d%d%d", &l,&r,&c) &&l&&r&&c) the { the      for(i =0; I < L; i++) the     { -          for(j =0; J < R; J + +) in         { thescanf"%s", Str[i][j]); the              for(k =0; K < C; k++) About             { the                 if(Str[i][j][k] = ='S') the                 { thebz=i; +bx=J; -by=K; the                 }Bayi             } the         } the     } -ans=BFS (bz,bx,by); -     if(ANS) theprintf"escaped in%d minute (s). \ n", ans); the     Else theprintf"trapped!\n"); the  - } the}

POJ 2251-dungeon Master (three-dimensional space for shortest path)

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.