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 
#include 
#include 
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;
};
queueS;
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;i1)//循环搜索出口
        {
            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;
}

意见反馈 || 任何建议

联系我(新浪)
邮箱:[email protected]

你可能感兴趣的:(ACM 之 C - Dungeon Master)