poj 2251 Dungeon Master(bfs)

来源:互联网 发布:lemon incest知乎 编辑:程序博客网 时间:2024/05/16 08:46
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17264 Accepted: 6722

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


题意:

给你n多层楼,求S到E的最小时间。

题解:

bfs,只不过是加了上下两个搜索方向。


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;}


0 0