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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • KMP算法:next和nextval值计算

    KMP算法:next和nextval值计算KMP算法的next和nextval值计算先看看next数据值的求解方法例:下标从1开始(若题中给定下标为0开始,把所有值-1即可)next数组的求解方法:根据前一个字符next,一直循环找到第

    2022年7月2日
    25
  • 四旋翼飞行器图片(4轴飞行器飞行运动中有)

    1、未知区域地形的构建2、农业方面的应用:(1)运用图像识别技术检测果实数量;(2)检测作物是否发生病虫害,因为当作物出现病虫害时都会有相应的表现现状。具体见链接http://www.aiweibang.com/yuedu/153474153.html3、高层建筑物的搭建

    2022年4月15日
    47
  • 人力资源管理中的大数据应用之道[通俗易懂]

    人力资源管理中的大数据应用之道[通俗易懂]本文来自网易云社区。随着时代的发展,计算机技术已经成为了人们生活以及日常办公必不可少的重要手段,尤其是近两年来,大数据以及云计算已经成为了企业管理的重要手段,不仅帮助企业提升业务管理,同样对于企业的人力资源管理同样起着重要的作用。从当前时代发展的角度来看,利用大数据进行人力资源分析,能够更好的帮助人力资源部门进行人员的招聘、人才的测评以及对人才进行合理的培训、管理、薪酬的配比以及员工的职业生涯…

    2022年5月27日
    36
  • 搭建服务器上的GIT并实现自动同步到站点目录(www)「建议收藏」

    搭建服务器上的GIT并实现自动同步到站点目录(www)

    2022年2月11日
    73
  • Js判断数组中是否存在某个元素「建议收藏」

    Js判断数组中是否存在某个元素「建议收藏」方法一:indexOf(item,start);Item:要查找的值;start:可选的整数参数,缺省则从起始位子开始查找。indexOf();返回元素在数组中的位置,如果没有则返回-1;例子:vararr=[‘aaa’,’bbb’,’ccc’,’ddd’,’eee’];  vara=arr.indexOf(‘ddd’);  console.log(a);  //3  varb=arr.indexOf(‘d’);  console.log(b);  //-1  我通常的用法:if(

    2022年10月19日
    3
  • Ubuntu 下 通过ADB 安装Apk和导出手机中的Apk

    Ubuntu 下 通过ADB 安装Apk和导出手机中的Apk一、连接电脑首先确保你的手机打开了调试模式然后输入命令adbdevicesroot@lvi166-CN15S:/home/lvi166#adbdevicesListofdevicesattachedce10171a39a990c00b7e device如果连接成功则会出现你的设备二、确认你要导出的apk包名root@lvi166-CN15S:/hom…

    2022年5月25日
    119

发表回复

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

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