POJ2251——Dungeon Master

Problem

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 <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char maze[31][31][31];
int vis[31][31][31];
int x, y, z;
int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {1,-1,0,0,0,0};
int sx, sy, sz;
int ex, ey, ez;
struct Point  {
    int x, y, z,step;
    Point(int x,int y,int z):x(x),y(y),z(z),step(0){};
    Point(){};
};

int BFS()  {
    memset(vis,0,sizeof(vis));
    queue<Point> q;
    Point node(sx,sy,sz), t;
    q.push(node);
    vis[sx][sy][sz] = 1;
    while(!q.empty())  {
        node = q.front();
        q.pop();
        for(int i = 0; i < 6; i++) {
            t = node;  t.x += dx[i];  t.y += dy[i];  t.z += dz[i];
            t.step++;
            if(t.x == ex && t.y == ey && t.z == ez)
                return t.step;
            if(t.x < 1 || t.x > x || t.y < 1 || t.y > y || t.z < 1 || t.z > z || !maze[t.x][t.y][t.z] || vis[t.x][t.y][t.z])
                continue;
            else  {
                vis[t.x][t.y][t.z] = 1;
                q.push(t);
            }
        }
    }
    return -1;
}

int main()  {
    char str[31];
    while(scanf("%d %d %d", &x, &y, &z) ==3 && (x+y+z))  {
        memset(maze,1,sizeof(maze));
        for(int i = 1; i <= x; i++)  {
            for(int j = 1; j <= y; j++)  {
                scanf("%s",str + 1);
                for(int k = 1; k <= z; k++)  {
                    if(str[k] == 'S')  {  sx = i;  sy = j; sz = k;  }
                    if(str[k] == 'E')  {  ex = i;  ey = j;  ez = k;  }
                    if(str[k] == '#')  maze[i][j][k] = 0;
                }
            }
        }
        if(BFS() == -1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n", BFS());
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,394评论 0 23
  • 莫道只如初见 故人心似离弦 闲愁这般入眉间 尘世几多愁怨 何叹秋风悲扇 平林新月寒蝉 世间惟有敬亭山 相看何曾相厌
    骊山语罢清宵半i阅读 2,372评论 1 4
  • 这是美国著名的摄影记者罗伯特·卡帕的一句话,这句话在网络上广为流传。 像蚂蚁一样工作。 这或许是我们协调工作与生活...
    香之兰儿阅读 4,181评论 0 0
  • 下午五点多,来了一场不及时的大雨,黑压压的天空总算是憋不住了,看着淅淅沥沥的小雨,眼前有点模糊,有点惆怅。 对于新...
    夜初微凉阅读 682评论 0 0
  • 井底之蛙安然于一口井,会觉得跳出一口井很困难深海之鲨遨游四方,无所畏惧的在大风大浪中漂流 面对困难与挫折, 有一些...
    Cloudya云阅读 3,919评论 0 0