POJ 2251 Dungeon Master (BFS)

Source: Internet
Author: User

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17264 Accepted: 6722

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


Test instructions

Give you n multi-storey, ask S to e the minimum time.

Exercises

BFS is just adding up and down two search directions.


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI    = ACOs ( -1.0); typedef long long Ll;const int inf=1000010;using namespace std;struct node{int x, y, Z; int num;} Node S,e;int N,m,c,ans;char mp[33][33][33];bool vis[33][33][33];queue<node>a;int x[6]= { -1,0,1,0,0,0};int y[6]    = {0,1,0,-1,0,0};int z[6]= {0,0,0,0,1,-1};int bfs () {memset (vis,0,sizeof vis);    A.push (s);    Node T;        while (A.size ()) {T=a.front ();        Node TT;        A.pop ();        if (t.x==e.x&&t.y==e.y&&t.z==e.z) return t.num;        for (int i=0; i<6; i++) {    Tt.x=x[i]+t.x;            Tt.y=y[i]+t.y;            Tt.z=z[i]+t.z; if (tt.x>=0&&tt.x<c&&tt.y>=0&&tt.y<n&&tt.z<m&&tt.z>=0                &&!vis[tt.x][tt.y][tt.z]&&mp[tt.x][tt.y][tt.z]!= ' # ') {tt.num=t.num+1;                A.push (TT);            Vis[tt.x][tt.y][tt.z]=1; }}} return-1;}    int main () {//freopen ("test.in", "R", stdin);        while (cin>>c>>n>>m) {if (n==0&&m==0&&c==0) break;        GetChar (); for (int i=0, i<c; i++) {for (int j=0; j<n; J + +) {for (int k=0; k<m; k                    + +) {scanf ("%c", &mp[i][j][k]);                    if (mp[i][j][k]== ' S ') {s.x=i,s.y=j,s.z=k,s.num=0;                      } if (mp[i][j][k]== ' E ') {  E.x=i,e.y=j,e.z=k;            }} getchar ();        }} while (A.size ()) A.pop ();        int Num=bfs ();        if (num!=-1) printf ("escaped in%d minute (s). \ n", num);    else printf ("trapped!\n"); } return 0;}


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.