[kuangbin带你飞]专题一 简单搜索 - B - Dungeon Master

B - 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 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!

水题:三维bfs
code:
#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>

using namespace std;
typedef long long ll;
typedef struct{
    int x,y,z,wal;
}data;
data star;
int n,k,ex,ey,ez,lev,hang,lie;
char maze[35][35][35];
int vis[35][35][35];
int dir[6][3]={ {1,0,0} , {0,1,0} , {0,0,1} , {0,-1,0} , {0,0,-1} , {-1,0,0} };

int bfs()
{
    queue <data> q;
    q.push(star);
    
    while(q.size())
    {
        data now=q.front();
        q.pop();
        
        if(now.x==ex&&now.y==ey&&now.z==ez){
            return now.wal;
        }
        data nex;
        nex.wal=now.wal+1;
        for(int i=0;i<6;i++)
        {
            int a=now.x+dir[i][0],b=now.y+dir[i][1],c=now.z+dir[i][2];
            
            if(vis[a][b][c]==0)
                if( 1 <= a && a <= lev && 1 <= b && b <= hang && 1 <= c && c <= lie )
                    if( maze[a][b][c] == '.' || maze[a][b][c] == 'E' )
                    {
                        vis[a][b][c]=1;
                        nex.x=a,nex.y=b,nex.z=c;
                        q.push(nex);
                    }
        }
    }
    return 0;
}

int main(void)
{
    
    while(cin>>lev>>hang>>lie&&lev+hang+lie)
    {
        memset(vis,0,sizeof vis);
        
        for(int i=1;i<=lev;i++)
            for(int j=1;j<=hang;j++)
                scanf("%s",maze[i][j]+1);
        
        for(int i=1;i<=lev;i++)
            for(int j=1;j<=hang;j++)
                for(int q=1;q<=lie;q++)
                {
                    if( maze[i][j][q]=='S' ) {
                        star.x=i,star.y=j,star.z=q;
                        vis[i][j][q]=1;
                        star.wal=0;
                    }
                    if( maze[i][j][q]=='E' ) {
                        ex=i,ey=j,ez=q;
                    }
                }
        int ans=bfs();

        if ( ans ) printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    }
}

### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单搜索题目而言,在为期约两周的时间里完成了这部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值