POJ 2251 Dungeon Master <三维BFS水题>

来源:互联网 发布:中文编程系统 编辑:程序博客网 时间:2024/05/17 13:12

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 37966 Accepted: 14520

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


看到题 ,第一反应BFS ,但是这是一种多个区域的“走迷宫”问题,那该怎么办呢?

其实转化一下,就是一道简单的三维BFS 题目,道理很简单,方向数组改成6个方向,点的坐标设置x,y,z ,然后

套用普通的BFS就能过了


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<queue>using namespace std;int l,r,c;int sx,sy,sz,ex,ey,ez;struct Node{    int x,y,z,step;    bool friend operator < (Node a, Node b){    return a.step>b.step;}};int vis[50][50][50];int dir[][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};priority_queue<Node>q;char maps[50][50][50];int bfs(){    Node a;    a.x=sx;    a.y=sy;    a.z=sz;    a.step=0;    q.push(a);    while(!q.empty()){        Node tp=q.top(),tmp;        if(tp.x==ex&&tp.y==ey&&tp.z==ez){            return tp.step;        }        q.pop();        for(int i=0;i<6;i++)        {            tmp.x=tp.x+dir[i][0];            tmp.y=tp.y+dir[i][1];            tmp.z=tp.z+dir[i][2];            if(tmp.x<0||tmp.x>=l||tmp.y<0||tmp.y>=r||tmp.z<0||tmp.z>=c||vis[tmp.x][tmp.y][tmp.z]||maps[tmp.x][tmp.y][tmp.z]=='#')                continue;            vis[tmp.x][tmp.y][tmp.z]=1;            tmp.step=tp.step+1;            q.push(tmp);        }    }    return -1;}int main(){    while(~scanf("%d%d%d",&l,&r,&c)&&l+r+c)    {        memset(vis,0,sizeof(vis));        for(int i=0; i<l; i++)            for(int j=0; j<r; j++)            {                scanf("%s",maps[i][j]);                for(int k=0; k<c; k++)                    if(maps[i][j][k]=='S')                        sx=i,sy=j,sz=k;                    else if(maps[i][j][k]=='E')                        ex=i,ey=j,ez=k;            }        int ans=bfs();        if(ans==-1)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n",ans);    }    return 0;}


原创粉丝点击