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 ...
随机推荐
- MongoDB副本集的实现与维护实战
1.建立MongoDB副本集 现利用一台机器完成MongoDB副本集的建立 机器1:127.0.0.1:27017 机器2:127.0.0.1:27018 机器3:127.0.0.1:27019 在D ...
- as3延迟处理
查找关键字“flashplayer 弹性跑道” 每当一帧即将走完,FlashPlayer就要做些总结性工作(一次性地汇总变化),把这一帧当中发生的变化拿出来展示(渲染)一下. 如果它处理的事情少,工作 ...
- WC项目要求
#include "stdio.h" #include "string.h" #include "stdlib.h" int charcal ...
- Java Json Object 互转
官方网址: http://json-lib.sourceforge.net/ 需要准备的jar包 1. json-lib-2.4-jdk15.jar 目前最新版, 下载地址 http: ...
- 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误
project: blog target: how-to-resolve-cannot-find-function-addEventListener-error-in-selenium.md stat ...
- Eclipse使用指定JDK,无需配置Path变量
修改Eclipse安装目录下的eclipse.ini配置文件 将下面内容添加到文件的首部 -vmF:/Lunatic/IDE/JDK/jdk1.7.0_67/jdk1.7.0_67/bin/javaw ...
- 第一个structs+spring+hibernate的web程序
1. 数据库: Column Type Comment id int(11) Auto Increment name varchar(50) NULL url varchar(255) NUL ...
- Tips for VNCServer config
Tips for VNCServer After the ClearCase server reboot by Jingwei, my vncserver background process is ...
- KinerCode.js
1 /*验证码*/ function KinerCode(options) { this.opt = this.extend(true, this.options, options); this.op ...
- HDU 5386 暴力
题目 也是个坑题,可惜没有发现这是个水题,被矩阵的气势吓住了,其实后来做出来的人挺多,就应该想到没那么难了.(两个队友陷入DP无法自拔,没有想换题的打算). 题意:告诉初始矩阵,目的矩阵,告诉n个步骤 ...