ACM 之 C - Dungeon Master

原题链接poj

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重新
写了;
二、虽然不是大问题,但是在输入的时候换行符的格式要注意,记得用getchar()吃掉;
三、也是最重要的一点!!每个可行的分支在进入队列的时候一定要状态标记它!!不然会重复运行导致超时。

代码部分

#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
char map[31][31][31],cc;
int s1,s2,s3,x,y,z,a,b,c,finde,sum,fla[31][31][31];
struct node
{
    int x,y,z;
};
queue<node>S;
node chan[6]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)&&a&&b&&c)
    {
        getchar();//吃换行符
        memset(fla,0,sizeof(fla));
        memset(map,'#',sizeof(map));//数据清空
        for(int i=0;i<a;i++)
        {   for(int j=0;j<b;j++)
            {   for(int k=0;k<c;k++)
                {
                    scanf("%c",&map[i][j][k]);//地图输入
                    if(map[i][j][k]=='.'||map[i][j][k]=='E')
                        fla[i][j][k]=-1;
                    if(map[i][j][k]=='S')
                    {
                        s1=i;s2=j;s3=k;
                    }
                }getchar();
            }getchar();
        }
        node thes;
        thes.x=s1;thes.y=s2;thes.z=s3;
        while(!S.empty())//队列清空
        {
            S.pop();
        }
        node count={-1,0,0};
        S.push(count);
        S.push(thes);
        sum=-1;finde=0;
        while(S.size()>1)//循环搜索出口
        {
            if(S.front().x == -1){ sum++; S.push(S.front()); S.pop(); }//层数标记
            x = S.front().x; y = S.front().y; z = S.front().z;
            if(map[x][y][z]=='E'){finde=1;break;}
            node si;
            for(int kk=0;kk<6;kk++)
            {
                if(fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]==-1)
                {
                    si.x=x+chan[kk].x;
                    si.y=y+chan[kk].y;
                    si.z=z+chan[kk].z;
                    S.push(si);
                    fla[x+chan[kk].x][y+chan[kk].y][z+chan[kk].z]=1;//一定要在这里标记它!不然就可能超时。
                }
            }
            S.pop();
        }
        if(finde==1)
        {
            printf("Escaped in %d minute(s).\n",sum);
        }
        else
            printf("Trapped!\n");
    }
    return 0;
}

意见反馈 || 任何建议

联系我(新浪)
邮箱:qianlizhihao@gmail.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,129评论 0 23
  • 《乌合之众》以十八世纪欧洲战争为背景,分析当时作为主体参战的群众,他们的心理特征,借以表达对法国民族文明的失望与不...
    0121af403f65阅读 602评论 0 0
  • 像园子里最后一串葡萄 挂在藤间,缄默着不开口 雨一直下 一群雨邀约着另一群雨 不顾天地只管下 这样的天气 适合想一...
    一只玻璃鱼阅读 284评论 0 3
  • 2016年年末的午餐是和孩子的婆婆爷爷、外公外婆、小姨小姨爹一起吃的,餐后送了婆婆爷爷回家,爸爸也去办事,只...
    千吉change阅读 191评论 0 0
  • 说这些问题,未免有点政治化,但是人是一个大的家庭的一个元素,逃离不了这个问题。人类社会的演变是一个充满血腥的过程,...
    佛禅善心阅读 209评论 0 1