uva 532

  Dungeon Master 

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 Specification 

The input file 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 Specification 

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 <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <stack>
#include <list>
#include <sstream>
#include <cmath>

using namespace std;

#define ms(arr, val) memset(arr, val, sizeof(arr))
#define mc(dest, src) memcpy(dest, src, sizeof(src))
#define N 35
#define INF 0x3fffffff
#define vint vector<int>
#define setint set<int>
#define mint map<int, int>
#define lint list<int>
#define sch stack<char>
#define qch queue<char>
#define sint stack<int>
#define qint queue<int>

int dir[6][3] = {0, 0, -1, 0, -1, 0, 0, 1, 0, 0, 0, 1, -1, 0, 0, 1, 0, 0};
char mazes[N][N][N];
int l, r, c;

struct node
{
    int i, j, k, lay;
    node(int i = 0, int j = 0, int k = 0, int lay = 0): i(i), j(j), k(k), lay(lay)
    {}
    void set(int i, int j, int k)
    {
        this->i = i;
        this->j = j;
        this->k = k;
    }
};

node s;
queue<node> que;
void bfs()
{
    int ans = 0;
    que.push(s);
    mazes[s.i][s.j][s.k] = '#';
    node t;
    int ii, jj, kk, p;
    while (!que.empty())
    {
        t = que.front();
        que.pop();
        for (p = 0; p < 6; p++)
        {
            ii = t.i + dir[p][0];
            jj = t.j + dir[p][1];
            kk = t.k + dir[p][2];
            if (ii > 0 && jj > 0 && kk > 0 && ii <= l && jj <= r && kk <= c)
            {
                if (mazes[ii][jj][kk] == '.')
                {
                    mazes[ii][jj][kk] = '#';
                    que.push(node(ii, jj, kk, t.lay + 1));
                }
                else
                    if (mazes[ii][jj][kk] == 'E')
                    {
                        ans = t.lay + 1;
                        break;
                    }
            }
        }
    }

    while (!que.empty())
    {
        que.pop();
    }
    if (ans)
    {
        printf("Escaped in %d minute(s).
", ans);
    }
    else
        puts("Trapped!");
}

int main()
{
    while (scanf("%d%d%d", &l, &r, &c), l && r && c)
    {
        for (int i = 1; i <= l; i++)
        {
            for (int j = 1; j <= r; j++)
            {
                scanf("%s", mazes[i][j] + 1);
            }
        }
        int i = 1, j = 1, k = 1;
        while (true)
        {
            if (mazes[i][j][k] == 'S')
            {
                s.set(i, j, k);
                break;
            }
            k++;
            if (k > c)
            {
                j++;
                k = 1;
                if (j > r)
                {
                    i++;
                    j = 1;
                }
            }
            
        }
        bfs();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jecyhw/p/3914676.html