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 ...
随机推荐
- /etc/default/useradd
系统默认的shell在,/etc/default/useradd 中,添加用户的时候如果不指定shell,默认的shell就是该文件下制定的文件
- github入门之基本操作--4
1.初始化仓库 如果成功执行git init 命令,该目录下会生成一个.git的目录 2.查看仓库状态 *注: 实际工作中,git status使用次数非常多,一定要记住.因为当工作树和仓库被操作的过 ...
- JavaEE汇总
1.简述Spring. a) Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,其目的是解决企业应用开发的复杂性,能够使用基本的JavaBean代替EJB,并提供了 ...
- Redis监控之redis-live.conf配置
{ "RedisServers": [ { "server": "192.168.1.201", "port": 637 ...
- (一)SpringMVC之警告: No mapping found for HTTP request with URI
这个警告往往是因为url路径不正确. 所以从三个地方下手: 1.springmvc-config.xml中的配置handle,看看是不是因为handle没有配置导致的. 2.如果是使用注解的方式的话, ...
- Linux Device Driver 学习(1)
Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...
- Android(java)学习笔记147:自定义SmartImageView(继承自ImageView,扩展功能为自动获取网络路径图片)
1. 有时候Android系统配置的UI控件,不能满足我们的需求,Android开发做到了一定程度,多少都会用到自定义控件,一方面是更加灵活,另一方面在大数据量的情况下自定义控件的效率比写布局文件更高 ...
- WPF中在后台实现控件样式
加入现在有一个Button的样式如下: <Style TargetType="{x:Type Button}" x:Key="MyButton">. ...
- 怎么在webstorm中设置代码模板
大家都知道webstorm对程序员来说是一个很好用的IDE.我们输入几个关键字,webstorm就会给出提示,大大提高了我们的开发效率,可有时候webstorm的默认设置不能满足我们的个性化代码模板的 ...
- The US in understimating Huawei, says founder Ren zhengfei
Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...