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 ...
随机推荐
- Android 仿微信朋友圈添加图片
github地址(欢迎下载Demo) https://github.com/zhouxu88/WXCircleAddPic 老习惯,先上图,着急用的朋友,直接带走Demo,先拿来用吧,毕竟老板催的紧, ...
- IOSAutolayout
21:55:33前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...
- MySQL分表操作的例子
USE project;DROP PROCEDURE IF EXISTS project.delete_test_user;delimiter $$CREATE PROCEDURE project.d ...
- 当互联网遇上家装,十大家装O2O混战
2015年已过去大半,装修O2O就出现了新的局面:为数众多的家居网络平台在家装O2O领域还未站稳脚跟,新的入局者就打出超低价格登场.新老O2O家装大战迅速展开,除了拼价格还拼品牌和体验,家装O2O的好 ...
- Injection of autowired dependencies failed;错误解决
代码自动生成的时候可能出现这个问题,反正我是找了半天才发现.serviceimp层不要写抽象类的声明abstract,这个删掉.
- win10 多桌面 win+tab | ctrl+win+左右箭头
win10 多桌面 win+tab | ctrl+win+左右箭头
- 【倍增】7.11fusion
非常奇妙的倍增题 题目描述 知名科学家小A在2118年在计算机上实现了模拟聚变的过程.我们将她研究的过程简化.核子共有26种,可以用a到z共26个字母表示.核子聚变的过程可以用一个字符串描述.按照顺序 ...
- (63)zabbix low-level discover zabbix批量部署必备
1. 概述 <zabbix发现配置>server通过配置好的规则,自动添加host.group.template <zabbix Active agent自动注册>与disco ...
- Python操作12306抢票脚本
有一段时间没有使用Python了,前几天经朋友提起一篇关于用Python实现抢火车票的文章,百度了实现抢火车票的技术细节,网上却有不少资料,也不是新鲜的东西.在了解了一些技术手段,阅读了一些大神的博文 ...
- Shell脚本的条件测试与比较
Shell脚本的条件测试与比较 一.shell脚本的条件测试 通常,在bash的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结构执行不同的操作,有时也会与if等条件语句相结合,来完成测试 ...