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;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,197評論 0 23
  • 莫道只如初見 故人心似離弦 閑愁這般入眉間 塵世幾多愁怨 何嘆秋風悲扇 平林新月寒蟬 世間惟有敬亭山 相看何曾相厭
    驪山語罷清宵半i閱讀 332評論 1 4
  • 這是美國著名的攝影記者羅伯特·卡帕的一句話,這句話在網絡上廣為流傳。 像螞蟻一樣工作。 這或許是我們協(xié)調工作與生活...
    香之蘭兒閱讀 885評論 0 0
  • 下午五點多,來了一場不及時的大雨,黑壓壓的天空總算是憋不住了,看著淅淅瀝瀝的小雨,眼前有點模糊,有點惆悵。 對于新...
    夜初微涼閱讀 122評論 0 0
  • 井底之蛙安然于一口井,會覺得跳出一口井很困難深海之鯊遨游四方,無所畏懼的在大風大浪中漂流 面對困難與挫折, 有一些...
    Cloudya云閱讀 735評論 0 0

友情鏈接更多精彩內容