POJ 2251 Dungeon Master bfs 难度:0
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的更多相关文章
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- 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 ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- MySQL 慢查询日志工具之pt-query-digest
1. 工具简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tc ...
- django如何防止csrf(跨站请求伪造)
什么是CSRF 下面这张图片说明了CSRF的攻击原理: Django中如何防范CSRF Django使用专门的中间件(CsrfMiddleware)来进行CSRF防护.具体的原理如下: 1.它修改当前 ...
- Get,Post请求方式详解
前几天工作中,所有表单我都采用post方法,头儿说那样不好,大型网站上一般都采用get方法,不理解. 在网上摘到一段比较有用的话 减低服务器流量压力根据 HTTP 规范,GET 用于信息获取,而且应该 ...
- CSU 1642 Problem B[难][前缀和]
Description 已知两个正整数a和b,求在a与b之间(包含a和b)的所有整数的十进制表示中1出现的次数. Input 多组数据(不超过100000组),每组数据2个整数a,b.(1≤a,b≤1 ...
- python学习之路-day8
一.接口与归一化设计 1.什么是接口 调用某个功能的方法/方式/入口 2.为什么要用接口 接口提取了一群类共同的函数,可以把接口当做一个函数的集合. 然后让子类去实现接口中的函数. 这么做的意义在于归 ...
- cocos代码研究(8)持续动作子类学习笔记
理论部分 时间间隔动作(ActionInterval)是一个在一段时间内执行的动作. 它有一个开始时间和完成时间.完成时间等于起始时间加上持续时间. ActionInterval的子类与位置有关的动作 ...
- js文件被浏览器缓存
如果修改了js文件中的js代码,发布代码到线上后.用户的浏览器使用的还是原来js缓存.所以并不会马上生效. 如何才能让浏览器使用最新的js文件呢? 我去看了一下淘宝,发现也是这样一种方式额,不知道对不 ...
- 浅谈JS严格模式
浅谈JS严格模式 简介 何为严格模式?严格模式(strict mode)即在严格的条件下运行,在严格模式下,很多正常情况下不会报错的问题语句,将会报错并阻止运行. 但是,严格模式可以显著提高代码的健壮 ...
- 获取WebView加载的网页内容并进行动态修改
http://www.jianshu.com/p/3f207a8e32cb [Android]WebView读取本地图片 http://www.cnblogs.com/kimmy/p/4769788. ...
- 详解KMP算法【转】
本文转载自:http://www.cnblogs.com/yjiyjige/p/3263858.html KMP算法应该是每一本<数据结构>书都会讲的,算是知名度最高的算法之一了,但很可惜 ...