POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37296 | Accepted: 14266 |
Description
the maze is surrounded by solid rock on all sides.
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。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int x, y, z, step;
}; int L, R, C;
char m[MAXN][MAXN][MAXN];
int vis[MAXN][MAXN][MAXN];
int dir[][] = { {,,},{,,},{,,},{-,,},{,-,},{,,-} }; queue<node>que;
int bfs(node s, node e)
{
ms(vis,);
while(!que.empty()) que.pop(); s.step = ;
vis[s.x][s.y][s.z] = ;
que.push(s); while(!que.empty())
{
node now = que.front();
que.pop(); if(now.x==e.x && now.y==e.y && now.z==e.z)
return now.step; for(int i = ; i<; i++)
{
node tmp;
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.z = now.z + dir[i][];
if(tmp.x>= && tmp.x<=L && tmp.y>= && tmp.y<=R && tmp.z>= && tmp.z<=C
&& m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] )
{
vis[tmp.x][tmp.y][tmp.z] = ;
tmp.step = now.step + ;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d",&L, &R, &C) && (L||R||C))
{
for(int i = ; i<=L; i++)
for(int j = ; j<=R; j++)
scanf("%s", m[i][j]+); node s, e;
for(int i = ; i<=L; i++)
for(int j = ; j<=R; j++)
for(int k = ; k<=C; k++)
{
if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k;
if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k;
} int ans = bfs(s,e);
if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return ;
}
POJ2251 Dungeon Master —— BFS的更多相关文章
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ2251——Dungeon Master(三维BFS)
和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向. 用上队列的进出操作较为轻松. #include<iostream> #include& ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- poj 2251 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] 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 )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
随机推荐
- Fruit Ninja
Fruit Ninja 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 Fruit Ni ...
- 【BZOJ1305】dance跳舞(最大流,裂点,二分答案)
题意:一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲. 有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”). ...
- lnux 下 core文件
1. core文件的简单介绍在一个程序崩溃时,它一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的. 2. 开启或关闭core文件的生成用以下 ...
- Mac 快速修改 hosts 文件
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
- shell的case脚本的简单入门
shell的case脚本的简单入门 示例1: #/bin/bash a=$ case "$a" in ") echo 'hell 2';; ") echo 'h ...
- 社区发现(Community Detection)算法
作者: peghoty 出处: http://blog.csdn.net/peghoty/article/details/9286905 社区发现(Community Detection)算法用来发现 ...
- python3.x之print()
1.print内容 #!/usr/bin/python print('hello world') //print("hello world") 2.print变量 #!/us ...
- Java成长之路
怎样学习才能从一名Java初级程序员成长为一名合格的架构师,或者说一名合格的架构师应该有怎样的技术知识体系,这是不仅一个刚刚踏入职场的初级程序员也是工作三五年之后开始迷茫的老程序员经常会问到的问题.希 ...
- gorm 结构体 预加载
结构体构建 type PlansApproval struct { ID uint Plans_Id int //plans编号 UpdateUser int //更新者 ...
- react+flask+antd
待学习: 1.https://www.cnblogs.com/jlj9520/p/6625535.html 2.http://python.jobbole.com/87112/ 3.