POJ 2251 BFS(简单)
一道三维的BFS
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24003 Accepted: 9332
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
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
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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!
题意:
一个能向东西南北前后走的人,,”#“是墙,“.”是路,问从“S”到“E” 的最少步数。
一个明显的BFS ,,, 除了是三维的没有任何难度
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
char a[66][66][66];
int l,r,c,sx,sy,sz,vis[66][66][66];
int xx[]={1,-1,0,0,0,0},yy[]={0,0,1,-1,0,0},zz[]={0,0,0,0,1,-1};
int bfs()
{
queue<int> q,w,e;
q.push(sx);w.push(sy);e.push(sz);
while(!q.empty())
{
int x=q.front(),y=w.front(),z=e.front();
q.pop();w.pop();e.pop();
if(a[x][y][z]=='E') return vis[x][y][z];
for(int i=0;i<6;i++)
{
int dx=x+xx[i],dy=y+yy[i],dz=z+zz[i];
if(dx<1||dx>l||dy<1||dy>r||dz<1||dz>c||a[dx][dy][dz]=='#'||vis[dx][dy][dz])
continue;
q.push(dx);w.push(dy);e.push(dz);
vis[dx][dy][dz]=vis[x][y][z]+1;
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c)&&l)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
cin>>a[i][j][k];
if(a[i][j][k]=='S')
{
sx=i;sy=j;sz=k;
}
}
}
}
int k=bfs();
k?printf("Escaped in %d minute(s).\n",k):printf("Trapped!\n");
}
}
POJ 2251 BFS(简单)的更多相关文章
- Dungeon Master POJ - 2251(bfs)
对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...
- POJ - 2251 bfs [kuangbin带你飞]专题一
立体bfs,共有六个方向: const int dx[] = {0,0,1,-1,0,0}; const int dy[] = {1,-1,0,0,0,0}; const int dz[] = {0, ...
- POJ 2251 bfs
DESCRIPTION:给你一个三维的迷宫.问你是否能从起点走到终点.如果能,输出最小步数.对我来说难得就是我没有想到怎么把他给你的三维图转换成map.恩..好像解题报告上说.只要是这种的最短路都要用 ...
- 【BFS】POJ 2251
POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
随机推荐
- 安卓自动化测试:Android studio 自带的 Record Espresso Test || [ Appium & (Android studio || Python|| Eclipse ) ]
1.Android studio 自带的 Record Espresso Test https://developer.android.com/studio/test/espresso-test-r ...
- iframe 简单的一个用法 局部调用
<iframe id="main_com" name="main_com" width="100%" height="750 ...
- leetcode日记 Combination sum IV
题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...
- Devexpress DateEdit控件的值不反馈到数据源的处理方式。
如果在GridControl中要把编辑的值反馈到数据源,可以用Gridview1.PostEdit()方法. 可是在datalayout中使用就会遇到一些问题:比如说DateEdit控件,在保存数据的 ...
- ASP.NET 配置文件笔记
ASP——config配置文件: WebConfig 是一个XML文件 configuration根节点<appSettings>//应用程序设置,可以定义应用程序全局常量设置信息< ...
- jQuery深层次复制对象
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min. ...
- Promise与Defer认识
1.deffer对象:jquery的回掉函数解决方案:含义是延迟到未来某个点再执行: 2.$.ajax链式写法: $.ajax("test.php") .done(func ...
- img标签中的alt属性在IE6/7/8中的兼容问题
W3C HTML 4.01 规范规定,alt 属性指定了在 User Agents 不能显示图片.表单和 applets 的时候显示的替换文字.alt 属性在 IE6 IE7 IE8(Q) 下具有双重 ...
- ajax 中boolean值技巧
// 利用判断 数据重复 function checkId () { var flag = true; $.ajax({ url: "", type: "post&quo ...
- xmlrpc实现bugzilla api调用(无会话保持功能,单一接口请求)
xmlrpc实现bugzilla4 xmlrpc api调用(无会话保持功能,单一接口请求),如需会话保持,请参考我的另外一篇随笔(bugzilla4的xmlrpc接口api调用实现分享: xml ...