POJ 2251 Dungeon Master (BFS)

Source: Internet
Author: User

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17465 Accepted: 6792

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!

Source

ULM Local 1997 searches for water problems, but it is important to note that there is not only a downward walk but also a situation where the upper level goes.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stdlib.h>#include<queue>#include<stack>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn= -+5;Const intinf=0x3f3f3f3f;Const Doubleeps=1e-9;intdir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};intdir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};intdir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};intF,n,m,vis[maxn][maxn][maxn],ans;CharMAP[MAXN][MAXN][MAXN];structnode{intZ,x,y; intstep; BOOL operator< (ConstNode A)Const    {        returnStep>A.step; }}star,en;BOOLJudgeintXintYintz) {    if(0<=x && x<n &&0<=y && y<m &&0<=z && z<f &&!vis[z][x][y] && map[z][x][y]!='#')        return true; return false;}intBFS () {memset (Vis,0,sizeof(VIS)); Priority_queue<node>Q;    Q.push (Star); VIS[STAR.Z][STAR.X][STAR.Y]=1;  while(!Q.empty ())        {node Now,next; now=Q.top ();        Q.pop (); //printf ("(%d)%d)%d\n", now.z,now.x,now.y,now.step);        if(now.z==en.z && now.x==en.x && now.y==en.y)returnNow.step;  for(intI=0;i<4; i++) {Next.x=now.x+dir4[i][0]; Next.y=now.y+dir4[i][1]; Next.z=now.z; if(judge (next.x,next.y,next.z)) {Vis[next.z][next.x][next.y]=1; Next.step=now.step+1;            Q.push (next); }} next.z=now.z+1; Next.x=now.x; Next.y=now.y; if(judge (next.x,next.y,next.z)) {Vis[next.z][next.x][next.y]=1; Next.step=now.step+1;        Q.push (next); } next.z=now.z-1; Next.x=now.x; Next.y=now.y; if(judge (next.x,next.y,next.z)) {Vis[next.z][next.x][next.y]=1; Next.step=now.step+1;        Q.push (next); }    }    returnINF;}intMain () { while(SCANF (" %d%d%d", &f,&n,&m) && (f| | n| |m) {memset (map,0,sizeof(map));  for(intI=0; i<f;i++)             for(intj=0; j<n;j++) {cin>>Map[i][j];  for(intk=0; k<m;k++)                 {                     if(map[i][j][k]=='S') {Star.z=i; Star.x=J; Star.y=K; Star.step=0; }                     if(map[i][j][k]=='E') {en.z=i; En.x=J; EN.Y=K; }}} ans=BFS (); if(Ans==inf) printf ("trapped!\n"); Elseprintf"escaped in%d minute (s). \ n", ans); }    return 0;}
View Code

POJ 2251 Dungeon Master (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.