POJ 2251 Dungeon Master【BFS】
题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出。
学习的第一题三维的广搜@_@
过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴)
另外这一题的输入的方式还要再多看看--@_@--
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 55
using namespace std;
int map[maxn][maxn][maxn],vis[maxn][maxn][maxn];
int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
int l,r,c,flag;
char str[10005];
struct node
{
int x,y,z;
int step;
} st,en;
queue<node> q;
void bfs(int x,int y,int z)
{
node now,next;
while(!q.empty()) q.pop();//调用前清空队列
now.x=x;now.y=y;now.z=z;now.step=0;
q.push(now);
vis[x][y][z]=1;
while(!q.empty())
{
now=q.front();q.pop();
for(int i=0;i<6;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.z=now.z+dir[i][2];
if(map[next.x][next.y][next.z]&&!vis[next.x][next.y][next.z])
{
vis[next.x][next.y][next.z]=1;
next.step=now.step+1;//步数加1之后再如队列,因为 搞反 这个wa了好几次
q.push(next);
if(next.x==en.x&&next.y==en.y&&next.z==en.z)
{
flag=1;
en.step=next.step;
return;
}
}
}
}
return;
}
int main()
{
char ch;
int i,j,k;
while(scanf("%d %d %d",&l,&r,&c)!=EOF&&l&&r&&c)
{
flag=0;
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis)); gets(str);
for(i=1;i<=l;i++)
{
for(j=1;j<=r;j++)
{
for(k=1;k<=c;k++)
{
scanf("%c",&ch);
if(ch=='S') {st.x=i;st.y=j;st.z=k;}
if(ch=='E'){en.x=i;en.y=j;en.z=k;}
if(ch!='#') map[i][j][k]=1;//map数组 相当于限定是否出界
}
gets(str);
}
gets(str);
}
bfs(st.x,st.y,st.z);
if(flag) printf("Escaped in %d minute(s).\n",en.step);
else printf("Trapped!\n");
}
}
go---go-----===
POJ 2251 Dungeon Master【BFS】的更多相关文章
- POJ - 2251 Dungeon Master 【BFS】
题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- (简单) POJ 2251 Dungeon Master,BFS。
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- 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: ...
随机推荐
- jdbc14 及 jdbc16 共存所带来的问题【未完待续】
在JAVA中JDK版本号与JDBC版本号的一致性十分重要,开发都们经常会忽略了这一点导致非常多不必要的错误. 昨天给客户排查了一个关于EDB在JBoss中使用时关于这方面的问题,希望给大家一点启示. ...
- Asp.net button防止点击多次数据提交
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat= ...
- SVN Commit报错 svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted
svn commit 文件出错 svn: E155037: Commit failed (details follow): svn: E155037: Previous operation has n ...
- 51nod-1503 猪和回文 - 二维矩阵上的dp
题目链接 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表示. 刚开 ...
- 四个例子实战讲解.htaccess文件rewrite规则(转)
一.防盗链功能 1 2 3 4 RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC] Rewrite ...
- Python内置数据结构之列表list
1. Python的数据类型简介 数据结构是以某种方式(如通过编号)组合起来的数据元素(如数.字符乃至其他数据结构)集合.在Python中,最基本的数据结构为序列(sequence). Python内 ...
- 解决new Date的值为Invalid Date、NaN-NaN的问题
错误代码: let timespan = 1515239514230; let dateTime = new Date(timespan); console.log(dateTime) // 返回 I ...
- [codevs3657]括号序列
题目大意:有一列只有'(',')','[',']'构成的括号序列,求在序列中至少加上多少括号,能使该序列合法. 解题思路:区间dp. 我们以$f[i][j]$表示把区间$[i,j]$添成合法括号所需的 ...
- python基础5(文件操作,with语句)
打开文件 #使用 open f = open('路径',mode = '打开模式', encoding='编码') #可以使用with语句打开,不需要关闭,可以同时打开多个文件 with open(' ...
- 浅析为什么 char 类型的范围是 : -128~+127
在 C 语言中. signed char 类型的范围为 -128~127,每本教科书上也这么写.可是没有哪一本书上(包含老师)也不会给你为什么是 -128~127,这个问题貌似看起来也非常easyea ...