POJ 2251-Dungeon Master(BFS)[通俗易懂]

POJ 2251-Dungeon Master(BFS)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17007   Accepted: 6620

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
3维地图给起点和终点搜最短路。

。刷dfs专题刷出来这玩意也是醉了。。

BFS暴搜
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,m,sx,sy,sz,A,B,C;
bool vis[105][105][105];
char ma[105][105][105];
struct node
{
    int x,y,z,step;
};
int mv[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
void bfs()
{
    queue <node> Q;
    node  t;
    t.x=sx;t.y=sy;t.z=sz;t.step=0;
    vis[sx][sy][sz]=1;
    Q.push(t);
    while(!Q.empty())
    {
        node f=Q.front();Q.pop();
        if(ma[f.x][f.y][f.z]=='E')
        {
			printf("Escaped in %d minute(s).\n",f.step);
			return ;
        }
        for(int i=0;i<6;i++)
        {
            t.x=f.x+mv[i][0];
            t.y=f.y+mv[i][1];
            t.z=f.z+mv[i][2];
            if(0<=t.x&&t.x<A&&0<=t.y&&t.y<B&&0<=t.z&&t.z<C&&!vis[t.x][t.y][t.z]&&ma[t.x][t.y][t.z]!='#')
            {
                vis[t.x][t.y][t.z]=1;
                t.step=f.step+1;
                Q.push(t);
            }
        }
    }
    puts("Trapped!");
}
int main()
{
    while(scanf("%d%d%d",&A,&B,&C)!=EOF)
    {
    	if(!A&&!B&&!C)break;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<A;i++)
            for(int j=0;j<B;j++)
				scanf("%s",ma[i][j]);
		for(int i=0;i<A;i++)
			for(int j=0;j<B;j++)
				for(int k=0;k<C;k++)
				if(ma[i][j][k]=='S')
			    {
					sx=i;sy=j;sz=k;
					break;
		        }
            bfs();
    }
    return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116741.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DNS服务器配置和测试

    DNS服务器配置和测试DNS服务器配置和测试

    2022年5月16日
    51
  • html样式表优点,css样式表的使用有哪些优点?

    html样式表优点,css样式表的使用有哪些优点?CSS全称CascadingStyleSheet,表示层叠样式表,是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化CSS用于改进HTML标记内容的呈现。使用CSS我们可以基于媒体定义不同的内容显示方式。CSS能够对网页中元素位置的排版进行像素级精确…

    2022年7月25日
    6
  • memorycleaner汉化版(v4l2 userptr)

    本文链接:https://blog.csdn.net/coroutines/article/details/70141086可参考:http://www.it610.com/article/4522348.htm//v4l2官方翻译基于V4L2的应用,通常面临着大块数据的读取与拷贝等问题。尤其在嵌入式系统中,对于实时性能要求较高的应用,拷贝会花上几十个ms…

    2022年4月16日
    131
  • js二维码生成器_url生成二维码

    js二维码生成器_url生成二维码二维码又称QRCode,是一个近几年来移动设备上很流行的一种编码方式它比传统的一维码(条形码)能存更多的信息,也能表示更多的数据类型。按照一定规律排列组成的几何图形构成,它巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念生活中的应用也是非常的广泛人们的生活方方面面都离不开二维码,而且她也给人们带来了极大的便利。<br><br>(二维码自动识别)二维码有哪些优缺点:优点:1.高密度编码,信息容量大。 2.编码范围广。 3.容错能力强,..

    2022年10月10日
    0
  • Vagrant系列(二)—-Vagrant的配置文件Vagrantfile详解

    Vagrant系列(二)—-Vagrant的配置文件Vagrantfile详解

    2021年11月8日
    80
  • 查看php-fpm的进程和端口号

    查看php-fpm的进程和端口号

    2021年10月19日
    127

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号