D - (热身)简单宽搜回顾

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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!
解析见代码:

/*
三维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的更多相关文章

  1. hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  3. POJ3185(简单BFS,主要做测试使用)

    没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...

  4. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  5. hdu1312 Red and Black 简单BFS

    简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...

  6. 逃脱 (简单BFS)

    题目传送门 G逃脱  题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园 ...

  7. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  8. Dungeon Master (简单BFS)

    Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...

  9. hdu - 2102 A计划 (简单bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以. 用一个3维字符数组存储图 ...

随机推荐

  1. Heroku使用

    先要生成一个公钥,使用命令:$ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save ...

  2. C#模拟登录的htmlHelper类

    public class HTMLHelper { /// <summary> /// 获取CooKie /// /// </summary> /// /// <para ...

  3. 自律训练法 John Sehorz

    自律训练法,系1932年由德国精神医学医师John Sehorz所创立.他研究人们在催眠催眠状态下,所呈现的生理状态,如:沉重与温暖感.. ,因而,John Sehorz改以「逆向操作」之方式,由自我 ...

  4. Scala学习笔记--Akka

    待完成 http://www.gtan.com/akka_doc/ http://my.oschina.net/mingdong/blog/297972 http://www.jdon.com/con ...

  5. Linux_access the file or directory which start with "-"

    cd -- filename/dirName [ccmobil@vm-a65a-fc19 wstemp]$ cd -/ -bash: cd: -: invalid option cd: usage: ...

  6. altium designer Summer09出现的问题解决方案

    在编译原理图时,引脚和连线旁边出现很多红线,提示 error:signal with no driver. 原理图没有加入到Project里. 第一次导入没问题,但是改了个元件的封装,在更新一下(De ...

  7. smarty 从配置文件读取变量

    smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...

  8. Google GFS文件系统深入分析

    Google GFS文件系统深入分析 现在云计算渐成潮流,对大规模数据应用.可伸缩.高容错的分布式文件系统的需求日渐增长.Google根据自身的经验打造的这套针对大量廉价客户机的Google GFS文 ...

  9. Linux企业级项目实践之网络爬虫(16)——使用base64传输二进制数据

    用http传输二进制的数据时,需要将二进制做一下转化,例如传输的int类型,将int类型之间转为char以后,丢失掉了长度的信息,如数字123456,本来只有4个字节,但是转化成文本的"12 ...

  10. jQuery手机触屏左右滑动切换焦点图特效代码

    原文地址:http://www.17sucai.com/pins/4857.html 演示地址:http://www.17sucai.com/pins/demoshow/4857 干净演示地址:htt ...