Dungeon Master POJ2251

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 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 <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 40;
int L, R, C;
char m[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};

struct node
{
    int x, y, z;
    int step;
}start, over;


void bfs(node a, node b)
{
    queue<node> q;
    q.push(a);
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        if(a.x == b.x && a.y == b.y && a.z == b.z)
        {
            cout << "Escaped in " << a.step << " minute(s)." << '\n';
            return ;
        }
        for(int i = 0; i < 6; ++i)
        {
            int xx = a.x + dx[i];
            int yy = a.y + dy[i];
            int zz = a.z + dz[i];
            if(xx > L || xx <= 0 || yy > R || yy <= 0 || zz > C || zz <= 0 )
                continue;
            if(m[xx][yy][zz] == '#' || vis[xx][yy][zz] == 1)
                continue;
            vis[xx][yy][zz] = 1;///漏了这句超时
            node tem;
            tem.x = xx;
            tem.y = yy;
            tem.z = zz;
            tem.step = a.step + 1;
            q.push(tem);
        }
    }
    cout << "Trapped!" << '\n';
}

int main()
{
    while(~scanf("%d%d%d", &L, &R, &C) && !(L == 0 && R == 0 && C == 0) )
    {
        getchar();
        for(int i = 1; i <= L; ++i)
        {
            for(int j = 1; j <= R; ++j)
            {
                for(int k = 1; k <= C; ++k)
                {
                    scanf("%c", &m[i][j][k]);
                    if(m[i][j][k] == 'S')
                    {
                        start.x = i;
                        start.y = j;
                        start.z = k;
                        start.step = 0;
                    }
                    if(m[i][j][k] == 'E')
                    {
                        over.x = i;
                        over.y = j;
                        over.z = k;
                    }
                }
                getchar();
            }
            getchar();
        }
        bfs(start, over);
        ///错了不知怎么错的,加上这两句就过了...
        memset(m, 0, sizeof(m));
        memset(vis, 0, sizeof(vis));
    }
    return 0;
}
全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
443173次浏览 4517人参与
# 春招别灰心,我们一人来一句鼓励 #
42122次浏览 537人参与
# 北方华创开奖 #
107468次浏览 600人参与
# 地方国企笔面经互助 #
7973次浏览 18人参与
# 同bg的你秋招战况如何? #
77083次浏览 569人参与
# 实习必须要去大厂吗? #
55804次浏览 961人参与
# 阿里云管培生offer #
120412次浏览 2220人参与
# 虾皮求职进展汇总 #
116163次浏览 886人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11668次浏览 289人参与
# 实习,投递多份简历没人回复怎么办 #
2454912次浏览 34860人参与
# 提前批简历挂麻了怎么办 #
149924次浏览 1978人参与
# 在找工作求抱抱 #
906096次浏览 9421人参与
# 如果公司给你放一天假,你会怎么度过? #
4762次浏览 55人参与
# 你投递的公司有几家约面了? #
33209次浏览 188人参与
# 投递实习岗位前的准备 #
1196021次浏览 18550人参与
# 机械人春招想让哪家公司来捞你? #
157643次浏览 2267人参与
# 双非本科求职如何逆袭 #
662359次浏览 7397人参与
# 发工资后,你做的第一件事是什么 #
12798次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35896次浏览 384人参与
# 简历中的项目经历要怎么写? #
86935次浏览 1516人参与
# 参加完秋招的机械人,还参加春招吗? #
20148次浏览 240人参与
# 我的上岸简历长这样 #
452049次浏览 8089人参与
牛客网
牛客企业服务