poj 2251 Dungeon Master()

来源:互联网 发布:目前java主流开发框架 编辑:程序博客网 时间:2024/04/30 11:50
 poj 2251
Dungeon Master
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 16485        Accepted: 6408

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!

题意:

3D的 迷宫  我们要找出路  用BFS 向上下左右前后走  从S 走到E

最少能走几步

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<queue>#define N 50using namespace std;struct node{    int x,y,z;};int aa[6][3]= {{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};int pace[N][N][N],vis[N][N][N];char map[N][N][N];int begini,beginj,begink,endi,endj,endk;int n,m,l;int f(int x,int y,int z){    if(!vis[x][y][z]&&x<=n&&x>=1&&y<=m&&y>=1&&z<=l&&z>=1&&map[x][y][z]!='#')        return 1;    return 0;}int bfs(int i,int j,int k){    int x,y,z;    queue<node> Q;    Q.x.push(i),Q.y.push(j),Q.z.push(k);    vis[i][j][k] = 1;    while(!Q.x.empty())    {        x = Q.x.front();        Q.x.pop();        y = Q.y.front();        Q.y.pop();        z = Q.z.front();        Q.z.pop();        if(x == endi&&y==endj&&z==endk)            return pace[x][y][z];        for(int i=0; i<6; i++)        {            int xx = x+aa[i][0];            int yy = y+aa[i][1];            int zz = z+aa[i][2];            if(f(xx,yy,zz))            {                Q.x.push(xx);                Q.y.push(yy);                Q.z.push(zz);                vis[xx][yy][zz] = 1;                pace[xx][yy][zz] = pace[x][y][z]+1;            }        }    }    return -1;}int main(){    int i,j,k;    while(cin >> n >> m >> l&&(n||m||l))    {        for(i=1; i<=n; i++)            for(j=1; j<=m; j++)                for(k=1; k<=l; k++)                {                    cin >> map[i][j][k];                    if(map[i][j][k]=='S')                    {                        map[i][j][k]='.';                        begini =  i;                        beginj = j;                        begink = k;                    }                    if(map[i][j][k]=='E')                    {                        map[i][j][k]='.';                        endi = i;                        endj = j;                        endk = k;                    }                }        memset(vis,0,sizeof(vis));        memset(pace,0,sizeof(pace));        int ans = bfs(begini,beginj,begink);        if(ans==-1)            cout << "Trapped!"<< endl;        else            printf("Escaped in %d minute(s).\n",ans);    }    return 0;}




0 0
原创粉丝点击