AYIT606第七周周赛(3维广搜+队列) D - Dungeon Master地下勇士


D - Dungeon Master
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2251

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!
分析:

发现队列是一个很好地算法,很多问题通过队列去解决就变得没有那么复杂,是一个值得我好好去研究练习的算法。

这个题目的难度在于三维空间,不好去理解题意,分析去它的坐标变化,然后用结构体,三维数组,队列去处理,问题就好办多了!


#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct data
{
    int x,y,z,step;
} pre,cur;
int flag,n,m,k,sx,sy,sz,v[120][120][120];
char map[110][110][110];
int dis[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
void BFS()
{
    queue<data>que;
    cur.x=sx;
    cur.y=sy;
    cur.z=sz;
    cur.step=0;
    que.push(cur);
    v[sx][sy][sz]=1;
    while(!que.empty())
    {
        cur=que.front();
        que.pop();
        if(map[cur.x][cur.y][cur.z]=='E')
        {
            flag=1;
            printf("Escaped in %d minute(s).\n",cur.step);
            return ;
        }
        for(int i=0; i<6; i++)
        {
            pre=cur;
            pre.x+=dis[i][0];
            pre.y+=dis[i][1];
            pre.z+=dis[i][2];
            if(pre.x>=0&&pre.x<n&&pre.y>=0&&pre.y<m&&pre.z>=0&&pre.z<k&&!v[pre.x][pre.y][pre.z]&&map[pre.x][pre.y][pre.z]!='#')
            {
                pre.step+=1;
                v[pre.x][pre.y][pre.z]=1;
                que.push(pre);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        if(!n&&!m&&!k)
            break;
        memset(v,0,sizeof(v));
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                scanf("%s",&map[i][j]);
                for(int s=0; s<k; s++)
                    if(map[i][j][s]=='S')
                    {
                        sx=i;
                        sy=j;
                        sz=s;
                    }
            }
        flag=0;
        BFS();
        if(!flag)
            printf("Trapped!\n");
    }
    return 0;
}



你可能感兴趣的:(AYIT606第七周周赛(3维广搜+队列) D - Dungeon Master地下勇士)