E - Dungeon Master BFS

[NWUACM] 
你被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成
你每次向上下前后左右移动一个单位需要一分钟
你不能对角线移动并且四周封闭
是否存在逃出生天的可能性?如果存在,则需要多少时间?

Input - 输入

  输入第一行是一个数表示空间的数量。
  每个空间的描述的第一行为L,R和C(皆不超过30)。
  L表示空间的高度。
  R和C分别表示每层空间的行与列的大小。
  随后L层地牢,每层R行,每行C个字符。
  每个字符表示空间的一个单元。‘#‘表示不可通过单元,‘.‘表示空白单元。你的起始位置在‘S‘,出口为‘E‘。
  每层空间后都有一个空行。L,R和C均为0时输入结束。

Output - 输出

  每个空间对应一行输出。

  如果可以逃生,则输出如下

Escaped in x minute(s).

  x为最短脱离时间。

  如果无法逃生,则输出如下

Trapped!

Sample Input - 输入样例

3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output - 输出样例

Escaped in 11 minute(s).
Trapped!

思路:这个题目就是6个方向的BFS把 方向设定好,,找到起点找到终点,然后BFS吧
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstdio>
#include<cstring>
#define N 33
//int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
using namespace std;
int l,n,m;
char arr[N][N][N];
int mark[N][N][N];
int sa,sb,sc;
int ea,eb,ec;
struct stu{
    int a,b,c;//坐标
    int s;//距离
}e1,e2,e3;
int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };//6个方向
void BFS(){
    memset(mark,0,sizeof(mark));
    queue<stu >s;
    e1.a=sa,e1.b=sb,e1.c=sc;
    e1.s=0;
    s.push(e1);
    mark[sa][sb][sc]=1;

    int ans=-1;
    while(s.size()){
        e2=s.front();
        s.pop();
        if(e2.a==ea && e2.b==eb && e2.c==ec)//判断是否到达了终点
        {
            ans=e2.s;
            break;
        }
        for(int i=0;i<6;i++){
            e3.a=e2.a+base[i][0];
            e3.b=e2.b+base[i][1];
            e3.c=e2.c+base[i][2];
            if((e3.a>= 0) && (e3.a < l) && (e3.b >= 0) && (e3.b < n) && (e3.c >= 0) && (e3.c < m)
             && (!mark[e3.a][e3.b][e3.c]) && (arr[e3.a][e3.b][e3.c] == ‘.‘ || arr[e3.a][e3.b][e3.c] == ‘E‘))
            {
                e3.s=e2.s+1;
                mark[e3.a][e3.b][e3.c]=1;
                s.push(e3);
            }

        }
    }
    if(ans==-1){
        cout<<"Trapped!"<<endl;
    }
    else {
        printf("Escaped in %d minute(s).\n",ans);
    }
}

int main()
{
    while(cin>>l>>n>>m){
        if(n==0&&m==0&&l==0)
            break;
        for(int i=0;i<l;i++){
            for(int j=0;j<n;j++){
                scanf("%s",&arr[i][j]);
            }
        }
        for(int i=0;i<l;i++){
            for(int j=0;j<n;j++){
                for(int k=0;k<m;k++){
                    if(arr[i][j][k]==‘S‘)
                    {
                        sa=i;
                        sb=j;
                        sc=k;
                    }
                    else if(arr[i][j][k]==‘E‘){
                        ea=i;
                        eb=j;
                        ec=k;
                    }
                }
            }
        }
        BFS();
    }
    return 0;
}


原文地址:https://www.cnblogs.com/Accepting/p/11241617.html

时间: 2024-11-13 06:37:55

E - Dungeon Master BFS的相关文章

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

POJ 2251 Dungeon Master(bfs)

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 dow

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

[poj] Dungeon Master bfs

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

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

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

POJ:Dungeon Master(三维bfs模板题)

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

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

B - Dungeon Master 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 c