Dungeon Master(三维bfs)
Is an escape possible? If yes, how long will it take?
Input
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
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! 代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
char Map[][][];
int vis[][][];
struct node
{
int x,y,z;
int cnt;
};
using namespace std;
int sx,sy,sz;
int ex,ey,ez;
int dir[][]={{,,},{,,-},{-,,},{,,},{,,},{,-,}};
int s;
bool check(int x,int y,int z)
{
if((Map[z][x][y]=='.'||Map[z][x][y]=='E')&&vis[z][x][y]==)
{
return true;
}
else
{
return false;
}
}
bool bfs()
{
node start;
start.x=sx;
start.y=sy;
start.z=sz;
start.cnt=;
queue<node>q;
q.push(start);
vis[sz][sx][sy]=;
while(!q.empty())
{
node now=q.front();
q.pop();
//printf("%d %d %d\n",now.z,now.x,now.y);
if(now.z==ez&&now.x==ex&&now.y==ey)
{
s=now.cnt;
return true;
}
for(int t=;t<;t++)
{
node next;
next.x=now.x+dir[t][];
next.y=now.y+dir[t][];
next.z=now.z+dir[t][];
next.cnt=now.cnt+;
if(check(next.x,next.y,next.z))
{
vis[next.z][next.x][next.y]=;
q.push(next);
} }
}
return false;
}
int main()
{
int L,R,C;
while(scanf("%d%d%d",&L,&R,&C)!=EOF)
{
memset(vis,,sizeof(vis));
if(L==&&R==&&C==)
{
break;
}
s=;
for(int t=;t<L;t++)
{
for(int j=;j<R;j++)
{
scanf("%s",Map[t][j]);
}
}
for(int t=;t<L;t++)
{
for(int j=;j<R;j++)
{
for(int k=;k<C;k++)
{
if(Map[t][j][k]=='S')
{
sx=j;
sy=k;
sz=t;
}
if(Map[t][j][k]=='E')
{
ex=j;
ey=k;
ez=t;
}
}
}
} if(bfs())
printf("Escaped in %d minute(s).\n",s);
else
{
printf("Trapped!\n");
}
} return ;
}
Dungeon Master(三维bfs)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
随机推荐
- 基于IDEA 代码提交Git
基于IDEA 代码提交Git 步骤 1 创建一个项目 2 点击 VCS --> Import init Version Control --> Create Git Repository ...
- spring-Sessions are not supported by the MongoDB cluster to which this client is connected
原因: 1.mongodb版本过低4.0以下不支持事务的情况下会报这个 2.安全认证问题参考如下连接 https://blog.csdn.net/ssehs/article/details/10530 ...
- 如何设置远程MongoDB!
默认情况下V服务连接着本地mongoDB服务,如果想连接到其他mongoDB服务,请按如下设置: 方法一:通过控制台修改 进入控制台 http://x.x.x.x:xxxx/system/consol ...
- Python中匿名函数与内置高阶函数详解
大家好,从今天起早起Python将持续更新由小甜同学从 初学者的角度 学习Python的笔记,其特点就是全文大多由 新手易理解 的 代码与注释及动态演示 .刚入门的读者千万不要错过! 很多人学习pyt ...
- Pytorch_第八篇_深度学习 (DeepLearning) 基础 [4]---欠拟合、过拟合与正则化
深度学习 (DeepLearning) 基础 [4]---欠拟合.过拟合与正则化 Introduce 在上一篇"深度学习 (DeepLearning) 基础 [3]---梯度下降法" ...
- 8、Builder 建造者模式 组装复杂的实例 创造型模式
1.什么是Builder模式 定义: 将一个复杂对象的构建与表示相分离,使得同样的构建过程可以创建不同的表示.大白话就是,你不需要知道这个类的内部是什么样的,只用把想使用的参数传进去就可以了,达到了解 ...
- 93复原IP地址。
from typing import List# 这道题不是很难,但是限制条件有很多.# 用递归的方法可以很容易的想到.只需要四层递归就好了.# 每次进行加上限制条件.过滤每一层就好了..class ...
- salesforce零基础学习(九十九)Git 在salesforce项目中的应用(vs code篇)
本篇参考: https://code.visualstudio.com/docs/editor/versioncontrol https://git-scm.com/doc https://git-s ...
- 高级搜索树-伸展树(Splay Tree)
目录 局部性 双层伸展 查找操作 插入操作 删除操作 性能分析 完整源码 与AVL树一样,伸展树(Splay Tree)也是平衡二叉搜索树的一致,伸展树无需时刻都严格保持整棵树的平衡,也不需要对基本的 ...
- Qt 信号发射部分 undefined reference to错误
在使用信号与槽很容易发生 undefined reference to 发射信号 ①继承QObject ②添加Q_OBJECT ③执行qmake ④构建 然后就可以运行啦!但是不知道是为什么,悄咪咪 ...