【POJ2251】Dungeon Master 三维 BFS Hash(12/1000)

来源:互联网 发布:羊绒 知乎 编辑:程序博客网 时间:2024/05/27 20:51

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 5
S….
.###.
.##..
###.#

#####
#####
##.##
##…

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

给人启发的一道题,从二维到了三维,其实就算到了四维空间,方法也差不多,让我们见识到了BFS+查重的强大之处。

10000*Z+100*X+Y,我的判重函数

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <set>const int N=30+10;using namespace std;struct state{    int z,x,y,step;};queue<state> Q;set<int> S;int L,R,C,goalx,goaly,goalz,X,Y,Z,code;char tmp;int dx[6]={1,-1,0,0,0,0},    dy[6]={0,0,1,-1,0,0},    dz[6]={0,0,0,0,1,-1};int a[N][N][N];void bfs(){    while(!Q.empty()){        state t=Q.front();Q.pop();        for(int i=0;i<6;i++){            X=t.x+dx[i];            Y=t.y+dy[i];            Z=t.z+dz[i];            if (a[Z][X][Y]==1||X<0||X>=R||Z<0||Z>=L||Y<0||Y>=C) continue;            code=10000*Z+100*X+Y;            if (X==goalx&&Y==goaly&&Z==goalz){                cout<<"Escaped in "<<t.step+1<<" minute(s)."<<endl;                return;            }            if (S.find(code)==S.end()){                S.insert(code);                state node={Z,X,Y,t.step+1};                Q.push(node);            }        }    }    cout<<"Trapped!"<<endl;}int main(){    while(cin>>L>>R>>C){        if (!L&&!R&&!C) break;        memset(a,0,sizeof(a));        S.clear();        while(!Q.empty()) Q.pop();        for(int i=0;i<L;i++){            for(int j=0;j<R;j++){                for(int k=0;k<C;k++){                    cin>>tmp;                    if(tmp=='S'){                        state first={i,j,k,0};                        Q.push(first);                        code=10000*i+100*j+k;                        S.insert(code);                        a[i][j][k]=0;                    }                    else if(tmp=='E'){                        goalz=i;                        goalx=j;                        goaly=k;                        a[i][j][k]=0;                    }                    else if(tmp=='#'){                        a[i][j][k]=1;                    }                    else if(tmp=='.'){                        a[i][j][k]=0;                    }                }            }        }        bfs();    }    return 0;}
原创粉丝点击