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. 禁止输入emoji表情

    三个文本框textField UITextView都要禁止苹果自带emoji 后来发现是原来写的方法不能覆盖所有的表情,新增的表情过滤不掉,只好再加了一个方法 http://www.jianshu.c ...

  2. CF909F AND-permutations 构造

    正解:构造 解题报告: 传送门! QAQ我jio得还挺难的,,,构造+数论什么的果然还是不适合灵巧这种菜菜啊QAQ 不过理解了的话也就没有那么难?所以还是港下QAQ 首先看task1 首先要发现一个, ...

  3. Linux下如何执行Shell脚本

    Linux下你可以有两种方式执行Shell脚本: 1.用shell程序执行脚本:根据你的shell脚本的类型,选择shell程序,常用的有sh,bash,tcsh等(一般来说第一行#!/bin/bas ...

  4. PAT 1142 Maximal Clique[难]

    1142 Maximal Clique (25 分) A clique is a subset of vertices of an undirected graph such that every t ...

  5. Codeforces Round #275 (Div. 2) 题解

    A 题: 说的是在(LR) 之间找出ab互质 bc 互质 ac 不互质的 3个数 数据量小直接暴力 #include <iostream> #include <cstdio> ...

  6. Python ConfigParser的使用

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  7. esLint参数设置

    package.json { "name": "testEsLint", "version": "1.0.0", &qu ...

  8. STM32示波器 信号发生器

    源: STM32示波器 信号发生器

  9. 从e.getMessage()为null看Java异常机制

    问题:自定义异常触发了,但是自定义的提示信息RuntimeException却没有带过来. throw new RuntimeException("不允许插入报价主项和报价子项同时重复的记录 ...

  10. 20145219《网络对抗》PC平台逆向破解

    20145219<网络对抗>PC平台逆向破解 shellcode注入 1.shellcode shellcode是一段代码,溢出后,执行这段代码能开启系统shell. 2.构造方法 (1) ...