POJ-2251-Dungeon Master 三维BFS

来源:互联网 发布:钓鱼软件制作 编辑:程序博客网 时间:2024/06/05 14:16

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39067 Accepted: 14878

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be 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 is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are 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 is not possible to escape, print the line
Trapped!

Sample Input

3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997

[Submit]   [Go Back]   [Status]   [Discuss]


题意:三维迷宫求给定起点S能否到达终点T,能则输出最短距离;


思路:涉及最短距离肯定是BFS,注意起点不一定在第一层,所以方向有6个;


AC Code:


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <string>#include <map>using namespace std;#define LL long long#define INF 0x3f3f3f3f#define fi first#define se second#define eps 1const int mod=1000000000+7;int l,r,c;char m[50][50][50];int dx[]={0,0,0,0,1,-1};int dy[]={1,-1,0,0,0,0};int dz[]={0,0,1,-1,0,0};struct point{    int x,y,z;    int dis;};int bfs(point x){    queue<point> que;    que.push(x);    m[x.z][x.x][x.y]='#';    while(!que.empty()){        point n=que.front(); que.pop();        for(int i=0;i<6;i++){            int x=n.x+dx[i];            int y=n.y+dy[i];            int z=n.z+dz[i];            if(z>=1&&z<=l&&x>=0&&x<r&&y>=0&&y<c&&m[z][x][y]!='#'){                if(m[z][x][y]=='E') return n.dis+1;                                       point t; t.x=x; t.y=y; t.z=z; t.dis=n.dis+1;                m[z][x][y]='#';                que.push(t);             }        }    }    return 0;}int main() {    while(~scanf("%d%d%d",&l,&r,&c)){        if(l==0&&r==0&&c==0) break;        for(int i=1;i<=l;i++){      //层数            for(int j=0;j<r;j++){  //每层行数                scanf("%s",m[i][j]);            }        }        point st;        for(int z=1;z<=l;z++){            for(int i=0;i<r;i++){                for(int j=0;j<c;j++){                    if(m[z][i][j]=='S'){                        st.x=i; st.y=j; st.z=z;                        st.dis=0;                    }                }            }        }        int ans=bfs(st);        if(ans==0) printf("Trapped!\n");        else printf("Escaped in %d minute(s).\n",ans );    }    return 0;}




原创粉丝点击