poj 2251 Dungeon Master

来源:互联网 发布:淘宝减肥产品哪个好 编辑:程序博客网 时间:2024/06/05 16:53

还是简单的搜索题,只是简单的把3二维转化成3维,多写几个方向和括号就OK了 QAQ~

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!

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <map>#include <set>#include <queue>#include <stack>#include <cmath>#define LL long long#define INF 0x3f3f3f3f#define RR freopen("in.txt","r",stdin)using namespace std;struct node{    int x,y,z;    int step;} s,e;int L,R,C;int Min_step;bool Map[33][33][33];bool vis[33][33][33];int Dir[][3] = {{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};void BFS(){    memset(vis,false,sizeof(vis));    queue<node >Q;    node b,c;    b.x = s.x;    b.y = s.y;    b.z = s.z;    b.step = 0;    vis[b.x][b.y][b.z] = true;    Q.push(b);    while(!Q.empty())    {        b = Q.front();        Q.pop();        if(b.x == e.x && b.y == e.y && b.z == e.z)        {            Min_step = b.step;            return ;        }        for(int i=0; i<6; i++)        {            c.x = b.x + Dir[i][0];            c.y = b.y + Dir[i][1];            c.z = b.z + Dir[i][2];            c.step = b.step + 1;            if(c.x>=1 && c.y>=1 && c.z>=1 && c.x<=R && c.y<=C && c.z<=L && Map[c.x][c.y][c.z] && !vis[c.x][c.y][c.z])            {                vis[c.x][c.y][c.z] = true;                Q.push(c);            }        }    }}int main(){    while(cin>>L>>R>>C && (L || R || C))    {        memset(Map,false,sizeof(Map));        char ch;        for(int k=1; k<=L; k++)        {            for(int i=1; i<=R; i++)            {                for(int j=1; j<=C; j++)                {                    cin>>ch;                    if(ch == 'S' || ch == 'E' || ch == '.')                    {                        Map[i][j][k] = true;                        if(ch == 'S')                        {                            s.x = i;                            s.y = j;                            s.z = k;                        }                        else if(ch == 'E')                        {                            e.x = i;                            e.y = j;                            e.z = k;                        }                    }                }            }        }        Min_step = INF;        BFS();        if(Min_step != INF)            printf("Escaped in %d minute(s).\n",Min_step);        else            printf("Trapped!\n");    }    return 0;}

0 0
原创粉丝点击