ZOJ 1940 Dungeon Master three-dimensional BFS

Source: Internet
Author: User

Dungeon MasterTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i6 4u

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!

#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100001Const intinf=0x7fffffff;//infinitely Largeintdx[6] = {0,0,-1,1,0,0};intdy[6] = {0,0,0,0,1,-1};intdz[6] = {1,-1,0,0,0,0};Charmap[ +][ +][ +];intvis[ +][ +][ +], L, R, C;structnode{intx, y, Z; intTime ;} St, Ed;queue<node>Q;BOOLCheckintXintYintz) {    if(x >=1&& x <= L && y >=1&& y <= R && z >=1&& Z <=C)return true; return false;}intBFS () {intx, y, Z, T, I;  while(!Q.empty ()) {Node TMP=Q.front ();        Q.pop (); X=tmp.x; Y=tmp.y; Z=tmp.z; T=Tmp.time;  for(i =0; I <6; i++)        {            intNX = x +Dx[i]; intNY = y +Dy[i]; intNZ = z +Dz[i]; if(!vis[nx][ny][nz] && map[nx][ny][nz]! ='#'&&Check (NX,NY,NZ)) {                if(NX = = ed.x && ny = ed.y && NZ = =ed.z)returnt+1; VIS[NX][NY][NZ]=1;                Node temp; Temp.x=NX; TEMP.Y=NY; Temp.z=NZ; Temp.time= T +1;            Q.push (temp); }        }    }    return-1;}intMain () { while(~SCANF ("%d%d%d", &l, &r, &c) && (L + R +C) {memset (Vis,0,sizeof(VIS)); intI, j, K;  for(i =1; I <= L; i++)        {             for(j =1; J <= R; J + +)            {                 for(k =1; K <= C; k++) {cin>>Map[i][j][k]; if(Map[i][j][k] = ='S') {St.x= i, St.y = j, st.z = K;st.time =0;                         Q.push (ST); VIS[I][J][K]=1; }                    Else if(Map[i][j][k] = ='E') ed.x= i, Ed.y = j, ed.z =K; }            }        }        intAns =BFS (); if(ans = =-1) printf ("trapped!\n"); Elseprintf ("escaped in%d minute (s). \ n", ans);  while(!q.empty ()) Q.pop (); }    return 0;}

ZOJ 1940 Dungeon Master three-dimensional 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.