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!

还是感觉广搜比深搜简单点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 35
using namespace std;
int n,m,k;
int x1,x2,y1,y2,z1,z2;
char map[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];
struct node
{
    int x,y,z,step;
    friend bool operator < (node a,node b)
    {
        return a.step>b.step;
    }
};
void bfs()
{
    int i,j;
    int move[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};
    priority_queue<node>q;
    node beg,end;
    beg.x=x1;
    beg.y=y1;
    beg.z=z1;
    beg.step=0;
    q.push(beg);
    vis[x1][y1][z1]=1;
    while(!q.empty())
    {
        end=q.top();
        q.pop();
        if(end.x==x2&&end.y==y2&&end.z==z2)
        {
            printf("Escaped in %d minute(s).\n",end.step);
            return ;
        }
        for(i=0;i<6;i++)
        {
            beg.x=end.x+move[i][0];
            beg.y=end.y+move[i][1];
            beg.z=end.z+move[i][2];
            if(!vis[beg.x][beg.y][beg.z]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&beg.z>=0&&beg.z<k&&map[beg.x][beg.y][beg.z]!='#')
            {
                map[beg.x][beg.y][beg.z]='#';
                beg.step=end.step+1;
                q.push(beg);
            }
        }
    }
    printf("Trapped!\n");
}
int main()
{
    int i,j,t,s;
    while(scanf("%d%d%d",&n,&m,&k)&&n!=0&&m!=0&&k!=0)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%s",map[i][j]);
            }
        }  
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                for(t=0;t<k;t++)
                {
                    if(map[i][j][t]=='S')
                    {
                        x1=i;y1=j;z1=t;
                    }
                    if(map[i][j][t]=='E')
                    {
                        x2=i;y2=j;z2=t;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        bfs();
    }
    return 0;
}

  

posted @   非我非非我  阅读(192)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· 卧槽!C 语言宏定义原来可以玩出这些花样?高手必看!
· langchain0.3教程:从0到1打造一个智能聊天机器人
点击右上角即可分享
微信分享提示