POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 48380 | Accepted: 18252 |
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!
Source
跟平常的bfs问题的区别就是方向有6个
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 35
using namespace std;
int dir[][]={{,,},{,,},{,,-},{,-,},{,,},{-,,}};//6个方向 东,南,西,北,上,下
char G[max_v][max_v][max_v];
int vis[max_v][max_v][max_v];
int sx,sy,sz,fx,fy,fz;
int n,m,k;
int ans;
struct node
{
int x,y,z;
int step;
};
bool check(node a)//检查该点合法性
{
if(a.x<||a.x>=n||a.y<||a.y>=m||a.z<||a.z>=k)
return false;
else if(G[a.z][a.x][a.y]=='#')
return false;
else if(vis[a.z][a.x][a.y])
return false;
else
return true;
}
void bfs(int x,int y,int z)
{
queue<node> q;
node p,next; p.x=x;
p.y=y;
p.z=z;
p.step=; q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==fx&&p.y==fy&&p.z==fz)
{
ans=p.step;
return ;
} for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][];
next.z=p.z+dir[i][]; if(check(next))
{
vis[next.z][next.x][next.y]=;
next.step=p.step+;
q.push(next);
} }
}
}
int main()
{
while(~scanf("%d %d %d",&k,&n,&m))
{
if(n==&&m==&&k==)
break; for(int z=;z<k;z++)
{
for(int i=;i<n;i++)
{
scanf("%s",G[z][i]);
for(int j=;j<m;j++)
{
if(G[z][i][j]=='S')
sx=i,sy=j,sz=z;
else if(G[z][i][j]=='E')
fx=i,fy=j,fz=z;
}
}
} memset(vis,,sizeof(vis));
ans=-; bfs(sx,sy,sz); if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return ;
}
POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)的更多相关文章
- 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 /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 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
随机推荐
- LOJ#6491. zrq 学反演(莫比乌斯反演 杜教筛)
题意 题目链接 Sol 反演套路题? 不过最后一步还是挺妙的. 套路枚举\(d\),化简可以得到 \[\sum_{T = 1}^m (\frac{M}{T})^n \sum_{d \ | T} d \ ...
- mootools vs jquery
大部分最近才剛接觸JavaScript的人會面臨到的困難是該選擇哪個套件(library)或是該先學哪個套件.如果你在一間公司裡上班,那麼可能公司已經有一套固定使用的套件,若是在這種情況下,問題就沒那 ...
- 新电脑装不了win7?来试试我的方法!
好久没写日记了,今天稍有时间来写个有关于硬件的技术贴. 前段时间换了个惠普暗影精灵二代,它的cpu代数如图所示: 用了几天系统自带win10,不同浏览器字体模糊的问题是个问题,故而想装 ...
- 解决 login.live.com onedrive.live.com 等微软国外网站打不开问题
下面就分享一下通过更改HOSTS文件的方式打开onedrive网页版的方法. C:\Windows\System32\drivers\etc目录下的hosts文件把它复制到D盘,再复制一份放到桌面上. ...
- Android的onCreateOptionsMenu()创建菜单Menu
android一共有三种形式的菜单: 1.选项菜单(optinosMenu) 2.上下文菜单(ContextMenu) 3.子菜 ...
- linux 用户配置文件及其相关目录
用户配置文件及其相关目录: /etc/passwd 用户信息文件/etc/shadow 影子文件/etc/group 组信息文件/etc/gshadow 组密码文件邮箱目录模板目录 /etc/pass ...
- 前端HTML空格与后台PHP utf-8空格
今天在处理html input输入框时,发现一个问题: 在用户名输入框中输入admin "'p(中间是一个空格),点保存后台提示数据保存成功,按理应该是未修改,通过chrome调试工具发现传 ...
- 【转】教你弄清 OSX 的睡眠模式,以及合法的禁止产生 sleepimage
原文链接 因为之前用的是网上流传的土法来禁止生成 sleepimage,尝到了苦头,而且2次! 大家知道 OSX 有几种睡眠模式,其中 hibernatemode 可以是 0 (传统睡眠方式,不生成 ...
- fastdfs搭建和使用
目录 前言 安装 安装插件 tracker storage 用自带的 client 进行测试 想要查看结果必须安装nginx 使用示例 引用 配置 使用 前言 参考网址 错误处理 安装 安装插件 yu ...
- [IIS | 用户权限] Connect as... 的设置
ApplicationPoolIdentity is actually the best practice to use in IIS7. It is a dynamically created, u ...