poj2251 三维简单BFS
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!
解析见代码:
/*
三维BFS,寻找最短路
注意事项:首先是初始化,每输入一组数据记得要先将队列清空
另外要注意因为字符输入比较多,还多有空行,一定要把必要的回车符吃掉
三维的BFS和二维的并没有多少区,就是标记数组和地图数组开成三维的就可以
状态的转移,从一个点因为是三维的,本来应该是有6个移动方向,但是因为出口肯定是在
上面,所以没有必要往下走
*/
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
bool visit[35][35][35];
char map[35][35][35];
int flag;
int l,r,c;
struct node
{
int x,y,z;
int s;
};
queue<node> q;
void init()
{
while(!q.empty())
q.pop();
memset(visit,false,sizeof(visit));
memset(map,'.',sizeof(map));
flag=0;
}
int main()
{
int i,j,k;
char ch;
while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))
{
init();
getchar();//吃回车
node start;
for(i=0;i<l;i++)
{ for(j=0;j<r;j++)
{ for(k=0;k<c;k++)//三维的输入
{
cin>>ch;
map[i][j][k]=ch;
if(ch=='S') //记录起点位置
{
start.x=i;
start.y=j;
start.z=k;
start.s=0;
}
}
getchar();
}
getchar();
}
q.push(start);
visit[start.x][start.y][start.z]=1;//标记
while(!q.empty())
{
node m=q.front();
q.pop();
if(map[m.x][m.y][m.z]=='E')
{
printf("Escaped in %d minute(s).\n",m.s);
flag=1;//已经找到
break;
}
m.s++;
node m2;
if(m.x-1>=0)
{
m2=m;
m2.x--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.x+1<l)
{
m2=m;
m2.x++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.y-1>=0)
{
m2=m;
m2.y--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.y+1<r)
{
m2=m;
m2.y++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.z-1>=0)
{
m2=m;
m2.z--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.z+1<c)//向上走,不会向下走
{
m2=m;
m2.z++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
}
if(!flag)
puts("Trapped!");
}
return 0;
}
poj2251 三维简单BFS的更多相关文章
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- LightOJ 1012 简单bfs,水
1.LightOJ 1012 Guilty Prince 简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...
- POJ3185(简单BFS,主要做测试使用)
没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- hdu1312 Red and Black 简单BFS
简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...
- 逃脱 (简单BFS)
题目传送门 G逃脱 题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园 ...
- Dungeon Master POJ-2251 三维BFS
题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- hdu - 2102 A计划 (简单bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以. 用一个3维字符数组存储图 ...
随机推荐
- typedef std::string AddressLines[4]定义了一个string数组,大小为4
int main() { typedef std::]; std::]; std::string *pal1 = new AddressLines; delete [] pal; delete [] ...
- (原)windows8.1上使用opencv for python
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6204100.html 参考网址: http://www.docs.opencv.org/master/ ...
- Go 解析JSON
JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,具有自我描述性且易于让人阅读.尽管JSON是JavaScript的一个子集,但JSON是独立于 ...
- Jquery简略API使用
都是个人随手笔记,既然开通了博客园就分享给大家.谨做为参考,具体大家自己测试以及使用 ★ $() ★ JQ的一个万能获取对象的函数(获取跟CSS获取元素是一样的)$(function(){}); 替代 ...
- 今日分享一点干货。PHP中课程表的实现。
首先贴代码,代码贴完再细说: 前段HTML: <div id="studentRead" class="reading" style="z-in ...
- App轮播图
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- C语言初学 数学中带根号的复杂计算问题
#include<stdio.h> #include<math.h> int main() { double a,b; scanf("%lf%lf",&am ...
- contentSize、contentInset和contentOffset区别
contentSize.contentInset和contentOffset区别 分类: iphone开发2011-12-05 21:49 23495人阅读 评论(4) 收藏 举报 uiviewios ...
- hdu Number Sequence
这道题是寻找规律.别的方法一般都是超时. #include <cstdio> #include <cstring> #include <algorithm> usi ...
- LeetCode_Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...