设为首页 加入收藏

TOP

532 - Dungeon Master
2014-11-23 19:01:52 来源: 作者: 【 】 浏览:6
Tags:532 Dungeon Master

Dungeon Master

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 Specification
The input file 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 Specification
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!题意:一个三维的空间里面,有一座牢房,其中#表示围墙。问从S出发,只能够向上下左右前后六个方向走,能否到达E。如果可以,那么最短的路径是多少


这其实就是把图的BFS拓展到空间上。是一道裸题

#include
#include
#include
#include
#include
using namespace std;
char dungeon[60][60][60];
int visit[60][60][60],l,r,c,s;
int x,y,z,fx,fy,fz;
int dir[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int in(int x, int y, int z)
{
    if (x>=0 && x=0 && y=0 && z q;
    temp.x=x; temp.y=y; temp.z=z; temp.s=0;
    q.push(temp);
    visit[x][y][z]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for (i=0; i<6; i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.z=now.z+dir[i][2];
            next.s=now.s+1;
            if (in(next.x,next.y,next.z) && !visit[next.x][next.y][next.z] && dungeon[next.x][next.y][next.z]!='#')
            {
                if (dungeon[next.x][next.y][next.z]=='E')
                {
                    s=next.s;
                    return;
                }
                q.push(next);
                visit[next.x][next.y][next.z]=1;
            }
        }
    }
    s=-1;
    return ;
}
int main ()
{
    int i,j,k;
    while(cin>>l>>r>>c)
    {
        if (l==0) break;
        memset(visit,0,sizeof(visit));
        for (i=0; i>dungeon[i][j][k];
                    if (dungeon[i][j][k]=='S')
                    {
                        x=i;
                        y=j;
                        z=k;
                    }
                    if (dungeon[i][j][k]=='E')
                    {
                        fx=i;
                        fy=j;
                        fz=k;
                    }
                }
            }
        }
        if (x==fx && y==fy && z==fz) s=0;
        else bfs(x,y,z);
        if (s>=0) printf("Escaped in %d minute(s).\n",s);
        else cout<<"Trapped!"< 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 1325 Machine Schedule 二分.. 下一篇poj 1180 斜率优化dp

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 教程 - W3Sch (2025-12-26 12:00:51)
·Python基础教程,Pyt (2025-12-26 12:00:48)
·神仙级python入门教 (2025-12-26 12:00:46)
·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)