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 ...
随机推荐
- spring boot实战(第二篇)事件监听
http://blog.csdn.net/liaokailin/article/details/48186331 前言 spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利 ...
- Tomcat中的Filter
Filter 节选部分源码.源码版本 Tomcat8.5 说明 filter 是 Servlet 规范 filter 是在 ,执行 Servlet.service方法之前执行 Filter相关接口 p ...
- Dubbo实践(一)入门示例
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候 ...
- PAT——1021. 个位数统计
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...
- 如何导出SHP文件中的点坐标?(ArcGIS10)
行政区域坐标,网上流传较广的版本是包括海域的,假如你仅仅想要把陆地边界绘出,那么怎么办呢? 现在讲一下用arcgis 10从shp线.面文件中获取对应区域的坐标呢?(点图层忽略第一步) 首先用在arc ...
- Android——sqlite3 基本命令操作
平时用到database的地方不多,这里记录一下shell终端下直接对db的基本操作! 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details ...
- git 设置只输入一次用户名和密码
https方式每次都要输入密码,非常不爽 按照如下设置可只输入一次 记住密码(默认15分钟): git config --global credential.helper cache 自己定义时间(一 ...
- 1006.Sign in and Sign out(25)—PAT 甲级
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- MySQL的主从复制+双主模式
MySQL的主从复制 部署环境: MySQL master 192.168.40.21 MySQL slave 192.168.40.22 思路: 当主MySQL上进行数据上的操作或者变化时,主My ...
- windows下开启 PHP扩展Redis
Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...