nyoj 353 3D dungeon

3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

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?

输入

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.

输出

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!

样例输入

3 4 5

S....

.###.

.##..

###.#

#####

#####

##.##

##...

#####

#####

#.###

####E

1 3 3

S##

#E#

###

0 0 0

样例输出

Escaped in 11 minute(s).

Trapped!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int MAXN = 35;
const int INF = 0xfffffff;

struct Node
{
    int x, y, z;
    int step;
    Node()
    {
        x = y = z = 0;
        step = 0;
    }
};

Node TargetPos;
char Graph[MAXN][MAXN][MAXN];
//int MinTime[MAXN][MAXN][MAXN];
int row, col, level;

int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};

bool Is_CanGo(Node CurPos)
{
    if(CurPos.x < 0 || CurPos.x >= level || CurPos.y < 0 || CurPos.y >= row || CurPos.z < 0 || CurPos.z >= col || Graph[CurPos.x][CurPos.y][CurPos.z] == ‘#‘)
        return false;
    return true;
}

int BFS(Node S)
{
    queue <Node> Que;
    Que.push(S);
    while(!Que.empty())
    {
        Node Curpos = Que.front();
        Que.pop();
        if(Curpos.x == TargetPos.x && Curpos.y == TargetPos.y && Curpos.z == TargetPos.z)
            return Curpos.step;
        for(int i = 0; i < 6; ++i)
        {
            Node t;
            t.x = Curpos.x + dir[i][0];
            t.y = Curpos.y + dir[i][1];
            t.z = Curpos.z + dir[i][2];
            t.step = Curpos.step + 1;
            if(Is_CanGo(t))
            {
                Que.push(t);
                Graph[t.x][t.y][t.z] = ‘#‘;
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d %d %d", &level, &row, &col) && (level + row + col))
    {
        Node StartPos;
        for(int i = 0; i < level; ++i)
        {
            for(int j = 0; j < row; ++j)
            {
                scanf("%s", Graph[i][j]);
                for(int z = 0; z < col; ++z)
                {
                    if(Graph[i][j][z] == ‘S‘)
                        StartPos.x = i, StartPos.y = j, StartPos.z = z;
                    else if(Graph[i][j][z] == ‘E‘)
                        TargetPos.x = i, TargetPos.y = j, TargetPos.z = z;
                }
                getchar();
            }
           if(i != level - 1)
                getchar();
        }
        StartPos.step = 0;
        int t;
        t = BFS( StartPos );
        if(t != -1)
            printf("Escaped in %d minute(s).\n", t);
        else
            printf("Trapped!\n");
    }
    return 0;
}

  

时间: 2024-08-05 11:14:58

nyoj 353 3D dungeon的相关文章

NYOJ 353 3D dungeon 【bfs】

题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动次数. 策略:简单的深搜. 注意:由于是求最少的移动次数.所以要从全部能到达的中选出最少的. 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using

NYOJ353 3D dungeon 【BFS】

3D dungeon 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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

NYOJ 搜索题目汇总 NYOJ 20、21、27、42、58、82、202、284、325、353、488、491、523、592、722

NYOJ 搜索题目汇总 NYOJ 20 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<algorithm> using namespace std; int pre[100005]; vector<int>v[100005];//存储每个结点相邻的边 void DFS(int

暑假集训(1)第三弹 -----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 m

hdu 2251 Dungeon Master bfs

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 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

Dungeon Master(BFS)

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 diagonal

Dungeon Master

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status 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

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 r