POJ——2251Dungeon Master(三维BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25379 | Accepted: 9856 |
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!
以前看这题很多次,都是不敢做,今天感觉有点灵感,毕竟SPFA刷了挺多的水题+图的遍历还是会一点的,因此我又来了,以前没做另外一个原因是智障地以为结构体自带了重载运算符号,结果一编译卧槽?怎么一大堆错。然后就不想搞了。最近机智了一点还是自己写点结构体重载吧。对于我这种DP和搜索不行的人来说1A还是不错的。果然BFS一般比DFS简单……结构体多加一个步数,这样就可以方便得得到最后的步数了。这个方法在BFS里感觉很好用。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
int l,n,m;
const int N=50;
struct info
{
int x;
int y;
int z;
int step;
};
info S,E;
info direct[6]={{1,0,0,1},{-1,0,0,1},{0,1,0,1},{0,-1,0,1},{0,0,1,1},{0,0,-1,1}};//方向数组
char pos[N][N][N];
int vis[N][N][N];
info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.z=a.z+b.z;
c.step=a.step+b.step;
return c;
}
bool operator==(const info &a,const info &b)
{
return (a.x==b.x&&a.y==b.y&&a.z==b.z);
}
inline bool check(const info &Q)
{
if((pos[Q.x][Q.y][Q.z]=='.'||pos[Q.x][Q.y][Q.z]=='E')&&(!vis[Q.x][Q.y][Q.z]))//这里要记得算上终点
return true;
return false;
}
int main(void)
{
int i,j,k;
while (~scanf("%d%d%d",&l,&n,&m)&&(l||n||m))
{
S.step=0;
MM(vis);
MM(pos);
for (i=0; i<l; i++)
{
for (j=0; j<n; j++)
{
for (k=0; k<m; k++)
{
cin>>pos[i][j][k];//cin会略过空格和回车,这样比较方便判断S和E的位置。
if(pos[i][j][k]=='S')
{
S.x=i;
S.y=j;
S.z=k;
}
else if(pos[i][j][k]=='E')
{
E.x=i;
E.y=j;
E.z=k;
}
}
}
}
int r=-1;
queue<info>Q;
Q.push(S);
vis[S.x][S.y][S.z]=1;
while (!Q.empty())
{
info now=Q.front();
Q.pop();
if(now==E)
{
r=now.step;//步数给答案
break;
}
for (i=0; i<6; i++)
{
info v=now+direct[i];
if(check(v))
{
vis[v.x][v.y][v.z]=1;
Q.push(v);
}
}
}
r==-1?puts("Trapped!"):printf("Escaped in %d minute(s).\n",r);
}
return 0;
}
POJ——2251Dungeon Master(三维BFS)的更多相关文章
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- 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 ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- POJ 2251-Dungeon Master (三维空间求最短路径)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2049— Finding Nemo(三维BFS)10/200
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- ios 自定义消息提示框
自定义提示框,3秒钟后自动消失.如上图显示效果. 提示框加载代码: - (void)viewDidLoad { [super viewDidLoad]; //将view背景颜色变更为黄色 self.v ...
- URAL 2047 Maths (数学)
对于一个数来说,它的除数是确定的,那么它的前驱也是确定的,而起点只能是1或2,所以只要类似筛法先预处理出每个数的除数个数 ,然后递推出每个数往前的延伸的链长,更新最大长度,记录对应数字.找到maxn以 ...
- FaceBook pop 动画开源框架使用教程说明
https://github.com/facebook/pop Pop is an extensible animation engine for iOS and OS X. In addition ...
- 手把手教你打造一个 Mac 风格的 Windows10(手动滑稽)
Mark https://www.sqlsec.com/2018/04/winmac.html 大佬写得很好,资瓷!! MyDock可能不是最新的,给出官方维护的网盘:https://pan.bai ...
- glob - 形成路径名称
描述 (DESCRIPTION) 很久以前 在 UNIX V6 版 中 有一个 程序 /etc/glob 用来 展开 通配符模板. 不久以后 它 成为 shell 内建功能. 现在 人们 开发了 类似 ...
- sql mybatis 使用concat乱码
先贴代码,这是sql查询里面 select id,name,sex,phone,present,adder, CONCAT("从业",experience,"年" ...
- 【Codeforces Rockethon 2014】Solutions
转载请注明出处:http://www.cnblogs.com/Delostik/p/3553114.html 目前已有[A B C D E] 例行吐槽:趴桌子上睡着了 [A. Genetic Engi ...
- QT+动手设计一个登陆窗口+布局
登陆窗口的样式如下: 这里面涉及着窗口的UI设计,重点是局部布局和整体布局, 首先在ui窗口上添加一个容器类(Widget),然后将需要添加的控件放置在容器中,进行局部布局(在进行局部布局的时候可以使 ...
- Hibernate 多表查询 - Criteria添加子字段查询条件 - 出错问题解决
Criteria 查询条件如果是子对象中的非主键字段会报 could not resolve property private Criteria getCriteria(Favorite favori ...
- Noip2011提高组 聪明的质监员
题目传送门 讲真,既然质监员这么聪明,为什么要让我们帮他设计程序? 所以还是叫ZZ的质检员吧 其实,我最想说的,不是这个题,而是这个\(\Sigma\)(一见 \(\Sigma\) 就懵逼系列) 这个 ...