POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 51402 | Accepted: 19261 |
Description
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! 题意: 在一个三维空间中,# 表示石块,不能走,. 表示路,可以走,求从 S 到 E 花费的最短时间,每走一步花费时间加 1 。
注意:
1.不要写 if(bfs()) printf("%d",bfs()),这种形式,要写一个中间变量 int ans = bfs() ,然后,if(ans) printf("%d",ans);
2.这是一个三维空间,有六个方向
3.不要只判断当前位置是不是被标记过,还要判断当前位置是不是石头(“#”);
#include<iostream>
#include<queue>
#include <cstring>
#include<cstdio> using namespace std; const int maxn = ;
int go[][] = {,,,,,-,-,,,,,,,,,,-,};
bool mark[maxn][maxn][maxn];
char maze[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
int L,R,C;
struct node
{
int x,y,z;
int time;
}; bool Isok(int x,int y,int z)
{
//别忘了判断是不是'#'
return x >= && x < L && y >= && y < R && z >= && z < C && !mark[x][y][z] && maze[x][y][z] != '#';
}
int bfs()
{
queue<node> q;
struct node cu,ne;
cu.x=sx;cu.y=sy;cu.z=sz;
cu.time = ;
mark[sx][sy][sz]=;
q.push(cu);
while(!q.empty())
{
cu = q.front();
q.pop();
if(cu.x==ex&&cu.y==ey&&cu.z==ez)
return cu.time;
for(int i=;i<;i++)
{
ne.x = cu.x + go[i][];
ne.y = cu.y + go[i][];
ne.z = cu.z + go[i][];
if(Isok(ne.x,ne.y,ne.z))
{
mark[ne.x][ne.y][ne.z] = ;
ne.time = cu.time + ;
q.push(ne);
}
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C))
{
memset(mark,,sizeof(mark));
memset(maze,,sizeof(maze));
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
scanf("%s",maze[i][j]);
getchar(); //有没有getchar()都能过
}
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
{
for(int k=;k<C;k++)
{
if(maze[i][j][k] == 'S')
{
sx=i;sy=j;sz=k;
}
else if(maze[i][j][k] == 'E')
{
ex=i;ey=j;ez=k;
}
}
}
}
//不要直接使用bfs()的返回值做判断和做printf输出变量。
int ans = bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
Ac代码
POJ 2251 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 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...
- (简单) POJ 2251 Dungeon Master,BFS。
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- POJ 2251 Dungeon Master【BFS】
题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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 ...
随机推荐
- Gradle Goodness: Renaming Files while Copying
With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...
- Notes 20180311 : String第三讲_深入了解String
很多前辈我可能对于我的这节文章很困惑,觉得String这个东西还有什么需要特别了解的吗?其实不然,String是一个使用十分频繁的工具类,不可避免地我们也会遇到一些陷阱,深入了解String对于我们避 ...
- selenium java maven 自动化测试(二) 页面元素获取与操作
在第一节中,我们已经成功打开了页面,但是自动化测试必然包含了表单的填写与按钮的点击. 所以在第二章中我以博客园为例,完成按钮点击,表单填写 还是以代码为准,先上代码: package com.ryan ...
- code#5 P3 我有矩阵,你有吗?
我有矩阵,你有吗? 时间限制: 1.0 秒 空间限制: 128 MB 相关文件: 题目目录 题目描述 企鹅豆豆手里有两个 01 矩阵 A 和 B.他可以进行两种操作: 选择 A 矩阵的一行,然后把 ...
- mysql 常用的时间日期函数小结
本文主要是总结一些常用的在实际运用中常用的一些mysql时间日期以及转换的函数 1.now() :返回当前日期和时间 select now(); //2018-04-21 09:19:21 2.cu ...
- 第三月 day03.笔记
函数在调用的时候回形成一个私有作用域,内部变量不会被外面访问,这种保护机制叫做闭包,这就意味着函数调用完了,这个函数形成的栈内存就会被销毁,但有时候我们不希望被销毁. * 函数归属谁和他的调用没有关系 ...
- [示例] 用代码设置 ListView 颜色 (只适用 Win 平台,无需修改官方源码)
如果可以使用代码随意设置 ListView 的颜色,而不用加载额外的 Style 及修改官方的源码,那该有多好?! 其实 Style 提供了很强了扩充性及可塑性,可以很容易的去操作它. 下面以 Lis ...
- 树莓派 raspberry系统 VNC View 连接 Cannot currently show the desktop 错误解决
https://www.raspberrypi.org/forums/viewtopic.php?t=216737 我是因为空间不够
- 安装xml2js出现npm ERR! code E404 npm ERR! 404 Not Found: event-stream@3.3.6
原因是npm源指向的问题 执行: npm config set registry https://registry.npmjs.org/
- SpringBoot入门案例——创建maven Module方式
最近看到一个大牛写的spring boot案例,链接贴这 https://github.com/ityouknow/spring-boot-examples.git 这里通过在maven里创建多个mo ...