junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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 <string.h>
struct node
{
    int x, y, z, step;
}a[27001];

char map[31][31][31];
int vis[31][31][31], next[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int v, n, m, result, flag, sx,sy,sz,ex,ey,ez;

void bfs(int x, int y, int z, int step)
{
    int head, tail, mx, my, mz, i;
    head = tail = 0;
    vis[x][y][z] = 1;
    a[head].x = x;
    a[head].y = y;
    a[head].z = z;
    a[head].step = step;
    while(head <= tail)
    {
        for(i=0; i<6; ++i)
        {
            mx = a[head].x + next[i][0];
            my = a[head].y + next[i][1];
            mz = a[head].z + next[i][2];
            if(mx<0||my<0||mz<0||mx>=v||my>=n||mz>=m)
                continue;
            else
            {
                if(!vis[mx][my][mz] && map[mx][my][mz] != '#')
                {
                    ++tail;
                    vis[mx][my][mz] = 1;
                    a[tail].x = mx;
                    a[tail].y = my;
                    a[tail].z = mz;
                    a[tail].step = a[head].step+1;
                    if(map[mx][my][mz] == 'E')
                    {
                        flag = 1;
                        result = a[tail].step;
                        return;
                    }
                }
            }
        }
        ++head;
    }
}
int main()
{
    int i, j, k;
    while(scanf("%d%d%d",&v,&n,&m),v+n+m)
    {
        memset(map, 0, sizeof(map));
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        flag = 0;
        for(i=0; i<v; ++i)
            for(j=0; j<n; ++j)
                scanf("%s",map[i][j]);
        for(i=0; i<v; ++i)
            for(j=0; j<n; ++j)
                for(k=0; k<m; ++k)
                if(map[i][j][k]=='S')
                {
                    sx = i;
                    sy = j;
                    sz = k;
                }
                else if(map[i][j][k]=='E')
                {
                    ex = i;
                    ey = j;
                    ez = k;
                }
        bfs(sx, sy, sz, 0);
        if(!flag)
            puts("Trapped!");
        else
            printf("Escaped in %d minute(s).\n",result);
    }
    return 0;
}



posted on 2017-01-09 22:16  junior19  阅读(177)  评论(0编辑  收藏  举报