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. 从零开始写JavaWeb框架(第一章节)

    买了本<从零开始写JavaWeb框架> 因为是第一次用IDEA,期间遇到很多问题,比如:怎么在IDEA中配置tomcat: 在IDEA界面的右上角点击: 点击+,选择Maven 到了如下界 ...

  2. 【Python】sasa版:文件中csv读取在写入csv读取的数据和执行是否成功。

    sasa写的文件(包含解析文字) # coding=utf- from selenium import webdriver from time import sleep import keyword ...

  3. React Native教程

    React Native 中文网  http://reactnative.cn/ 相关资料======================= React-Native学习指南 https://github ...

  4. 系统管理命令之who am i

    who am i 显示的是实际用户的用户名,即用户登陆的时候的用户ID.此命令相当于who -m. 用Linux的术语来解释就是:(实际用户=uid,即user id.有效用户=euid,即effec ...

  5. 跨域nginx,CORS

    浏览器的同源策略是浏览器上为安全性考虑实施的非常重要的安全策略.从一个域上加载的脚本不允许访问另外一个域的文档属性.举个例子:比如一个恶意网站的页面通过iframe嵌入了银行的登录页面(二者不同源), ...

  6. springcloud11----turbine

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  7. PHP中使用OpenSSL下openssl_verify验证签名案例

    使用OpenSSL那么需要先了解一下http://www.cnblogs.com/wt645631686/p/8390936.html <?php //demo $json = '{" ...

  8. P1131 [ZJOI2007]时态同步(树形dp)

    P1131 [ZJOI2007]时态同步 设$f[i]$为与$i$与最远的点的距离 在dfs时每次更新的时候顺便统计一下长度,不同的话就改成最长的那条并更新答案 #include<iostrea ...

  9. 再论Splay

    联赛前为了填知识点,简单学了一下比较常用的高级数据结构,都没有太深入的理解,于是现在重新搞一遍. 其实有了set和multiset,那么我们就没有再手写平衡树的必要了,所以treap的应用就相对于Sp ...

  10. img = img1*mask + img2*(1-mask) How do that ?

    原文地址:http://answers.opencv.org/question/160599/img-img1mask-img21-mask-how-do-that/ 如何提高一个简单操作的速度?最后 ...