poj2251 Dungeon Master

题目:

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 t
he 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!


poj2251.PNG

题目大意:
有一个地牢,分为L层,每层R行C列。有一个人从起点出发,可以在同层上下左右移动,也可以到底层的同行同列处,也可以到高层的同行同列出(空间的前后左右上下)。'.'表示可以通过,'#'表示不能通过。问他能否到达终点,能则输出时间,否则输出"Trapped!"。

其实这道题可以用BFS过,只不过是空间六个方向。

参考代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 50+5;
const int M = 30000+10;

int diry[6] = {-1, 0, 0, 1, 0, 0};//方向坐标(前四个);
int dirz[6] = {0, 1, -1, 0, 0, 0};//组合起来分别代表上右左下(前四个);
int dirx[6] = {0, 0, 0, 0, 1, -1};//空间的下与上;
int L, R, C;
char map[N][N][N];
bool vis[N][N][N];
struct node {
    int x, y, z;//记录该点的位置;
    int times;//记录当前已用的时间;
} que[M];

void init() {
    for (int i = 0;i < L;++i) {
        for (int j = 0;j < R;++j) {
            for (int k = 0;k < C;++k) {
                map[i][j][k] = '#';
            }
        }
    }
    memset(que, 0, sizeof(que));
    memset(vis, false, sizeof(vis));
}

node s;
void input() {
    for (int i = 0;i < L;++i) {
        for (int j = 0;j < R;++j) {
            cin >> map[i][j];
            for (int k = 0;k < C;++k) {
                if (map[i][j][k] == 'S') {
                    s.x = i;
                    s.y = j;
                    s.z = k;
                    s.times = 0;
                    vis[i][j][k] = true;    
                }
            }
        }
    }
}

void bfs() {
    int l = 0, r = 0;
    que[r++] = s;
    node p;
    bool flag = false;
    while (r > l) {//队列不为空;
        p = que[l++];
        if (map[p.x][p.y][p.z] == 'E') {//已经到达终点;
            flag = true;
            cout << "Escaped in " << p.times << " " << "minute(s)" << "." << endl; 
            break;
        }
        node q;
        for (int i = 0;i < 6;++i) {
            int nx, ny, nz;
            nx = p.x + dirx[i];
            ny = p.y + diry[i];
            nz = p.z + dirz[i]; 
            if (nx >= 0 && nx < L && ny >= 0 && ny < R && nz >= 0 && nz < C && map[nx][ny][nz] != '#' && !vis[nx][ny][nz]) {
                q.x = nx;
                q.y = ny;
                q.z = nz;
                q.times = p.times + 1;
                que[r++] = q;
                vis[nx][ny][nz] = true;         
            }       
        }   
    }
    if (!flag)
        cout << "Trapped!" << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    while (cin >> L >> R >> C) {
        if (L == 0 && R == 0 && C == 0) break;
        cin.get();
        init();
        input();
        bfs();
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,250评论 0 23
  • 回不去的岁月 父母在地里劳动 不分昼夜 我长在他们汗水下 直到有能力去了远方 我在城市里生活 写字楼里穿着西装 父...
    原始生命阅读 229评论 7 12
  • 如今我只想静静的 躺在一个人的身边, 任天上流云的影子 千年如一日的漂过我们的脸。 我们爱过又忘记 像青草生长,钻...
    Z钟钟阅读 2,558评论 0 0
  • 20年,当初的翩翩少年已是白发模样。 最近,当莱昂纳多和凯特·温斯莱在慈善晚会上再次相聚,相拥,让人难免流泪唏嘘。...
    她生活阅读 2,243评论 10 33
  • 企业级产品是一个和用户端产品相对应的庞大领域,包括很多不同领域、不同形式的产品,也包括多样化的商业模式。同时,企业...
    urdaddy阅读 583评论 0 4