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!

理解

這道題是三維地圖廣搜,我做這道題的時(shí)候遇到了很多麻煩:
一、開始用的深搜以為自己沒錯(cuò)一直改改改,后來在學(xué)長(zhǎng)的提醒下用bfs重新
寫了;
二、雖然不是大問題,但是在輸入的時(shí)候換行符的格式要注意,記得用getchar()吃掉;
三、也是最重要的一點(diǎn)!!每個(gè)可行的分支在進(jìn)入隊(duì)列的時(shí)候一定要狀態(tài)標(biāo)記它!!不然會(huì)重復(fù)運(yùn)行導(dǎo)致超時(shí)。

代碼部分

#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));//數(shù)據(jù)清空
        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())//隊(duì)列清空
        {
            S.pop();
        }
        node count={-1,0,0};
        S.push(count);
        S.push(thes);
        sum=-1;finde=0;
        while(S.size()>1)//循環(huán)搜索出口
        {
            if(S.front().x == -1){ sum++; S.push(S.front()); S.pop(); }//層數(shù)標(biāo)記
            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;//一定要在這里標(biāo)記它!不然就可能超時(shí)。
                }
            }
            S.pop();
        }
        if(finde==1)
        {
            printf("Escaped in %d minute(s).\n",sum);
        }
        else
            printf("Trapped!\n");
    }
    return 0;
}

意見反饋 || 任何建議

聯(lián)系我(新浪)
郵箱:qianlizhihao@gmail.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,787評(píng)論 0 23
  • 《烏合之眾》以十八世紀(jì)歐洲戰(zhàn)爭(zhēng)為背景,分析當(dāng)時(shí)作為主體參戰(zhàn)的群眾,他們的心理特征,借以表達(dá)對(duì)法國(guó)民族文明的失望與不...
    0121af403f65閱讀 578評(píng)論 0 0
  • 像園子里最后一串葡萄 掛在藤間,緘默著不開口 雨一直下 一群雨邀約著另一群雨 不顧天地只管下 這樣的天氣 適合想一...
    一只玻璃魚閱讀 208評(píng)論 0 3
  • 2016年年末的午餐是和孩子的婆婆爺爺、外公外婆、小姨小姨爹一起吃的,餐后送了婆婆爺爺回家,爸爸也去辦事,只...
    千吉change閱讀 178評(píng)論 0 0
  • 說這些問題,未免有點(diǎn)政治化,但是人是一個(gè)大的家庭的一個(gè)元素,逃離不了這個(gè)問題。人類社會(huì)的演變是一個(gè)充滿血腥的過程,...
    佛禪善心閱讀 203評(píng)論 0 1