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维字符数组存储图 ...
随机推荐
- 读书笔记_Effective_C++_条款二十五: 考虑写出一个不抛出异常的swap函数
在之前的理论上调用对象的operator=是这样做的 void swap(A& x) { std::swap(a, x.a); } A& operator=(const A& ...
- 讨论一下PHP相关技能的层次
1.PHP编程能力 由于PHP的入门较为简单,所以暂时只有熟悉和精通两个级别. 1.熟悉PHP:精通PHP语法,掌握常用的函数,熟悉PHP5下的OOP应用,这个是基础,也没什么好说的. 2.精通PHP ...
- dubbo No provider available for the service com.alibaba.dubbo.monitor.MonitorService from registry
No provider available for the service com.alibaba.dubbo.monitor.MonitorService from registry http:// ...
- 判断Python输入是否为数字
在接收raw_input方法后,判断接收到的字符串是否为数字 例如: str = raw_input("please input the number:") if str.isdi ...
- production stage
MP:mass production 批量生产 DV design verification 设计验证 EV engineering verification 工程样品验证 PV process ve ...
- 使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。
分布式微服务现在成为了很多公司架构首先项,据我了解,很多java公司架构都是 Maven+Dubbo+Zookeeper基础上扩展的. Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按 ...
- mysql日志文件相关的配置【2】
1.二进制日志是什么? mysql 的二进制日志用于记录数据库上做的变更. 2.二进制日志什么时间写到磁盘 1.总的来说二进制日志会在释放锁之前就写入磁盘.也就是说在commit完成之前:client ...
- 《VIM-Adventures攻略》 LEVEL 4、5
本文已转至http://cn.abnerchou.me/2014/03/10/46d23509/ 上一篇文章忘记说明文本编辑器的模式: 所有文本编辑器都至少有两种模式,编辑模式和控制模式.编辑模式就是 ...
- 智能卡安全机制比较系列(六) TimeCOS
TimeCOS是握奇公司推出的智能卡操作系统,也可以说是国内早期自己开发的为数不多的几款COS之一.当然随着后来国内公司对于CPU卡开发的投入,其他公司的COS产品也纷纷推出. 其实从握奇的TimeC ...
- android面试题之六
三十一.AIDL的全称是什么?如何工作?能处理哪些类型的数据? 英文全称:Android Interface Define Language(Android接口定义语言). 当A进程要去调用B进程中的 ...