POJ 2251 Dungeon Master (three-dimensional 6-direction BFS)

Source: Internet
Author: User

B-dungeon MasterTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2251Appoint Description:System Crawler (2015-03-28)

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!

Main topic:

Give you a 3-dimensional maze, the dimension is L*r*c, tells you the beginning of the place and the place of termination, each time only to move to 6 directions, ask whether you can go out, if you can, find this

The minimum time.

Problem Solving Ideas:

Direct BFS on the shortest time problem, len[rear] = len[front]+1, every time before the team to update the path is good.

Code:

# include<cstdio># include<iostream># include<algorithm># include<functional># include<cstring># include<string># include<cstdlib># include<iomanip># include<numeric># include<cctype># include<cmath># include<ctime># include<queue># include<stack># include<list># include<Set># include<map>using namespacestd;Const DoublePi=4.0*atan (1.0); typedefLong Longll;typedef unsignedLong Longull;# define INF999999999# define MAX -CharGrid[max][max][max];intBook[max][max][max];intlen[max*max*MAX];intl,r,c;intans;intst_x,st_y,st_z;inted_x,ed_y,ed_z;intnext[6][3] = {{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};structnode{intx; inty; intZ;} Que[max*max*MAX];voidinput () { for(inti =0; I < l;i++ )        {             for(intj =0; J < r;j++ )            {                 for(intK =0; k < c;k++ )                {                    Charch; CH=GetChar (); GRID[I][J][K]=ch; if(ch=='S') {st_x=i; St_y=J; St_z=K;            }} getchar ();        } getchar (); }}intBFs () {intfront,rear; memset (book,0,sizeof(book)); memset (Len,0,sizeof(len)); que[0].x =st_x; que[0].Y =st_y; que[0].z =st_z; Rear= Front =0;  while(Front <=rear) {         for(inti =0; I <6; i++ )        {            intn_x = que[front].x+next[i][0]; intn_y = que[front].y+next[i][1]; intN_z = que[front].z+next[i][2]; if(book[n_x][n_y][n_z]==0&& (grid[n_x][n_y][n_z]=='.'|| grid[n_x][n_y][n_z]=='E') &&n_x>=0&&n_x<l&&n_y>=0&&n_y<r&&n_z>=0&&n_z<c) {Book[n_x][n_y][n_z]=1; que[++rear].x =n_x; Que[rear].y=n_y; Que[rear].z=n_z; Len[rear]= len[front]+1; if(Grid[n_x][n_y][n_z] = ='E' )                {                    returnLen[rear]; } }} front++; }    return 0;}intMainvoid){     while(cin>>l>>r>>c) {if(l==0&&r==0&&c==0 )             Break;        GetChar ();        Input (); Ans=BFS (); if(ANS) printf ("escaped in%d minute (s). \ n", ans); Elseprintf ("trapped!\n"); }    return 0;}
View Code

POJ 2251 Dungeon Master (three-dimensional 6-direction BFS)

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.