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条款15,在资源管理类中小心copying行为
class A { private: int *p; void lock(){ cout << p << "is lock" << endl; ...
- UVa 10098: Generating Fast
这道题要求按字典序生成字符串的全排列,不可重复(但字符可以重复,且区分大小写). 基本思路是先对输入的字符串按字典序排序,然后从第一位开始递归,从所有输入的字符中选出一个填充,然后再选第二位..... ...
- Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试
在前端开发中,尤其是MVC架构多人开发,负责前端的童鞋总是需要做静态页面,再和后台连接前无法使用变量如EL表达式等测试功能,所以本人引入了一个模板jsp数据测试专用文件,专门配置所有的变量,然后在待测 ...
- PHP获取图片颜色值,检测图片主要颜色的代码:
<?php $i=imagecreatefromjpeg("photo3.jpg");//测试图片,自己定义一个,注意路径 for ($x=0;$x<imagesx($ ...
- canvas个人总结
今天做了大量的canvas作业,发现很多的步奏都是一样的,我自己就封装了一个画直线形2D图形函数.功能不是很强大. function drawModule(Json,strokeStyle,fillS ...
- 移动端远程关闭PC端实现(一)需求设计
公司有台半新不旧的电脑,因无甚大用,就拿来做了服务器,服务于民.服务器所提供的功能不是太多,无非是数据库以及svn服务. 公司每天下班会断电,我们吧会常常忘记关闭服务器,所以服务器非正常关机的次数约等 ...
- jquery easyui根据需求二次开发记录
1.tree需要显示多个图标 实际需求:设备树上节点需搁三个图片,分别标识运行状态.告警状态.设备类型 解决方法:给tree的iconCls传入一个数组,分别是各状态下的class(css),然后要改 ...
- Haskell趣學指南--这个有意思
正在慢慢了解不同于命令式的函数式语言. 希望能获得新的视野.. ~~~~~~~~~~~ http://learnyouahaskell-zh-tw.csie.org/zh-cn/ready-begin ...
- UART与USART的区别
UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...
- SWF加解密资源索引之加密混淆篇【转】
============================ SWF加解密资源索引之加密混淆篇 ============================ [心得] swf加密混淆器(带源码) http:/ ...