http://poj.org/problem?id=2251

bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct P{
int x,y,z;
P(){x=y=z=0;}
P(int x,int y,int z){
this->x=x;this->y=y;this->z=z;
}
};
const int dx[6]={1,-1,0,0,0,0};
const int dy[6]={0,0,1,-1,0,0};
const int dz[6]={0,0,0,0,1,-1}; int l,r,c;
char maz[30][30][31];
int step[30][30][30];
bool in(int z,int x,int y){
return z>=0&&z<l&&
x>=0&&x<r&&
y>=0&&y<c;
}
int main(){
while(scanf("%d%d%d",&l,&r,&c)==3&&l){
memset(step,0x7f,sizeof(step));
for(int i=0;i<l;i++){
for(int j=0;j<r;j++){
scanf("%s",maz[i][j]);
}
}
queue<P> que;
for(int i=0;i<l;i++){
for(int j=0;j<r;j++){
for(int k=0;k<c;k++){
if(maz[i][j][k]=='E'){
que.push(P(j,k,i));
step[i][j][k]=0;
}
}
}
}
bool fl=false;
while(!que.empty()){
P f=que.front();que.pop();
int z=f.z,x=f.x,y=f.y;
if(maz[z][x][y]=='S'){
fl=true;
printf("Escaped in %d minute(s).\n",step[z][x][y]);
break;
}
for(int i=0;i<6;i++){
int tx=x+dx[i],ty=y+dy[i],tz=z+dz[i];
if(in(tz,tx,ty)&&maz[tz][tx][ty]!='#'&&step[tz][tx][ty]>step[z][x][y]+1){
step[tz][tx][ty]=step[z][x][y]+1;
que.push(P(tx,ty,tz));
}
}
}
while(!que.empty())que.pop();
if(!fl)puts("Trapped!");
}
return 0;
}

  

POJ 2251 Dungeon Master bfs 难度:0的更多相关文章

  1. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  2. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  4. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  7. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  8. POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)

    POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...

  9. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 浅谈vuex

    很多技术,刚接触的时候:这是啥?用的时候:哟嚯,是挺好用的!加以研究:卧槽,就是这么个逼玩意儿! 最近接手了一个别人写了1/5的vue项目(页面画了1/3,接口啥都没对); 对于表格中的数据项操作以及 ...

  2. Python开发【模块】:M2Crypto RSA加密、解密

    M2Crypto 模块 快速安装: # 环境centos7.0,提前装好openssl(自行百度安装),windows装不上,暂不考虑了 [root@localhost ~]# pip install ...

  3. 创建WCF服务的过程

    一.创建控制台WCF工程 1.创建一个控制台工程2.System.ServiceModel的引用3.可创建多个WCF服务,如:IService.cs和Service.cs    顺序:右键->添 ...

  4. js-jquery-从SweetAlert到SweetAlert2

    原文地址:https://github.com/limonte/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2 1. IE supp ...

  5. (转) 密码学中的“盐值 Salt”

    为什么要在密码里加点“盐” 盐(Salt) 在密码学中,是指通过在密码任意固定位置插入特定的字符串,让散列后的结果和使用原始密码的散列结果不相符,这种过程称之为“加盐”. 以上这句话是维基百科上对于 ...

  6. ubuntu16.04 安装指定版本Node,升级npm到指定版本

    一.安装配置Node 1.下载(64位系统) wget https://nodejs.org/download/release/v10.1.0/node-v10.1.0-linux-x64.tar.g ...

  7. java多线程(五)

    Java 多线程同步 锁机制与synchronized 打个比方:一个object就像一个大房子,大门永远打开.房子里有很多房间(也就是方法).这些房间有上锁的(synchronized方法), 和不 ...

  8. SpringData_PagingAndSortingRepository接口

    该接口提供了分页与排序功能 Iterable<T> findAll(Sort sort); //排序 Page<T> findAll(Pageable pageable); / ...

  9. cocos代码研究(24)Widget子类PageView学习笔记

    理论基础 PageView类又称Layout的管理器,可以让用户在多个Layout之间左右或者上下切换显示,继承自 Layout . 代码实践 static PageView * create ()创 ...

  10. 2018 Multi-University Training Contest 10 Solution

    A - Problem A.Alkane 留坑. B - Problem B. Beads 留坑. C - Problem C. Calculate 留坑. D - Problem D. Permut ...