POJ - 2251 地下城主
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
| Inputcopy | Outputcopy |
|---|---|
3 4 5 |
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAX=35;
int L,R,C;
char graph[MAX][MAX][MAX];//graph为地图
bool vis[MAX][MAX][MAX];//vis为标记
int beginx,beginy,beginz,endx,endy,endz;//分别记录起点和终点坐标
int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//dir为6种走法
struct node{
int x,y,z;
int step;
};
void bfs(){
node d;
d.x=beginx,
d.y=beginy,
d.z=beginz;
d.step=0;
queue<node>q;
q.push(d);
vis[beginx][beginy][beginz]=true;
while(!q.empty()){
node n=q.front();
q.pop();
if(n.x==endx&&n.y==endy&&n.z==endz){
cout<<"Escaped in "<<n.step<<" minute(s)."<<endl;
return ;
}
for(int i=0;i<6;i++){
int x1=n.x+dir[i][0];
int y1=n.y+dir[i][1];
int z1=n.z+dir[i][2];
if(graph[x1][y1][z1]!='#'&&!vis[x1][y1][z1]&&x1>=0&&x1<L&&y1>=0&&y1<R&&z1>=0&&z1<C){
node n1;
n1.x=x1;
n1.y=y1;
n1.z=z1;
n1.step=n.step+1;
q.push(n1);
vis[x1][y1][z1]=true;
}
}
}
cout<<"Trapped!"<<endl;
}
int main(){
while(scanf("%d%d%d",&L,&R,&C)!=EOF){
memset(graph,'#',sizeof graph);
if(L==0&&R==0&&C==0)break;
for(int i=0;i<L;i++){
for(int j=0;j<R;j++){
cin>>graph[i][j];
for(int k=0;k<C;k++){
if(graph[i][j][k]=='S'){
beginx=i,beginy=j,beginz=k;
}
if(graph[i][j][k]=='E'){
endx=i,endy=j,endz=k;
}
}
}
}
memset(vis,0,sizeof vis);
bfs();
}
return 0;
}
POJ - 2251 地下城主的更多相关文章
- 洛谷 P2360 地下城主
P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...
- 【BFS】POJ 2251
POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)
对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- MySQL学习(五)事务
参考博客:https://www.cnblogs.com/kismetv/p/10331633.html 0.提交和回滚 注:mysql默认自动开启了事务. -- 手动开启事务 start trans ...
- 阿里云镜像创建Spring Boot工厂
参考博客:https://blog.csdn.net/qq_40052237/article/details/115794368 http://start.aliyun.com
- Weak Encryption 弱加密安全问题处理
Weak Encryption Abstract 程序使用了弱加密算法,无法保证敏感数据的保密性. Explanation 陈旧的加密算法(如 DES)再也不能为敏感数据提供足够的保护了. 加密算法依 ...
- MyBatis中的#和$有什么区别
什么是MyBatis MyBatis是一款优秀的持久层框架,特别是在国内(国外据说还是 Hibernate 的天下)非常的流行,我们常说的SSM组合中的M指的就是#mybatis#. MyBatis支 ...
- 【ASP.NET Core】在node.js上托管Blazor WebAssembly应用
由于 Blazor-WebAssembly 是在浏览器中运行的,通常不需要执行服务器代码,只要有个"窝"能托管并提供相关文件的下载即可.所以,当你有一个现成的 Blazor was ...
- DES算法流程
初始置换IP 表格的使用方法: 将输入的64bit的明文从1开始标号,依次放入到IP初始置换表中数字对应的位置.填充完毕后,按照行优先的顺序从第1行开始依次读取获得输出. 16轮轮结构 整体结构 因为 ...
- [BUUCTF]Pwn刷题记录
本部分内容长期更新,不再创建新文章影响阅读 rip 根据IDA加载入main函数声明发现s数组距离rbp的距离为F,即为15,这里的运行环境是64位,所以应当将Caller's rbp的数据填满,在这 ...
- 在EF Core中为数据表按列加密存储
假设有User表 public class User : Entity<int> { public int Id { get; set; } public string UserName ...
- Barplot/pie/boxplot作图详解——R语言
当数据以简单的可视化的形式呈现时,数据便更具有意义并且更容易理解,因为人眼很难从原始数据中得出重要的信息.因此,数据可视化成为了解读数据最重要的方式之一.条形图和箱线图是了解变量分布的最常用的图形工具 ...
- Python多线程与GIL锁
Python多线程与GIL锁 python多线程 Python的多线程编程可以在单个进程内创建多个线程来同时执行多个任务,从而提高程序的效率和性能.Python的多线程实现依赖于操作系统的线程调度器, ...