Dungeon Master POJ - 2251(bfs)
对于3维的,可以用结构体来储存,详细见下列代码。
样例可以过,不过能不能ac还不知道,疑似poj炸了,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
int bx, by, bz;
int ex, ey, ez;
const int maxn = 30 + 5;
class POS
{
public:
POS():x_(-1), y_(-1), z_(-1){ }
POS(int x, int y, int z):x_(x), y_(y), z_(z){ }
int getX(){ return x_;}
int getY(){ return y_;}
int getZ(){ return z_;}
private:
int x_, y_, z_;
};
typedef POS P;
struct Level
{
char maze[maxn][maxn];
int vis[maxn][maxn];
int d[maxn][maxn];
};
int L, R, C;
Level *h;
bool op(int x, int y, int z)
{
if(x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L)
return true;
return false;
}
int bfs()
{
queue<P> pos;
pos.push(P(bx, by, bz));
while(!pos.empty())
{
P p = pos.front();
pos.pop();
if(p.getX() == ex && p.getY() == ey && p.getZ() == ez)
break;
h[p.getZ()].vis[p.getX()][p.getY()] = 1;
for(int i = 0; i < 6; i++)
{
int curx = p.getX() + dx[i], cury = p.getY() + dy[i], curz = p.getZ() + dz[i];
if(op(curx, cury, curz) && h[curz].maze[curx][cury] != '#' && !h[curz].vis[curx][cury])
{
h[curz].vis[curx][cury] = 1;
pos.push(P(curx, cury, curz));
h[curz].d[curx][cury] = h[p.getZ()].d[p.getX()][p.getY()] + 1;
}
}
}
return h[ez].d[ex][ey];
}
int main()
{
while(cin >> L >> R >> C && (L || R || C))
{
bx = by = bz = -1;
ex = ey = ez = -1;
h = new Level[L];
for(int i = 0; i < L; i++)
{
for(int j = 0; j < R; j++)
{
scanf("%s", h[i].maze[j]); //input
for(int k = 0; k < C ; k++)
{
h[i].vis[j][k] = 0;
if(h[i].maze[j][k] == 'S')
{
h[i].d[j][k] = 0;
bx = j;
by = k;
bz = i;
}
else
h[i].d[j][k] = INF;
if(h[i].maze[j][k] == 'E')
{
ex = j;
ey = k;
ez = i;
}
}
}
}
int ans = bfs();
if(ans == INF)
cout << "Trapped!" << endl;
else
cout << "Escaped in " << ans << " minute(s)." << endl;
delete []h;
}
}
Dungeon Master POJ - 2251(bfs)的更多相关文章
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- Dungeon Master POJ - 2251 (搜索)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48605 Accepted: 18339 ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- B - Dungeon Master POJ - 2251
//纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...
- kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- poj 2251 Dungeon Master 3维bfs(水水)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21230 Accepted: 8261 D ...
随机推荐
- vs2013+cocos2d-x-2.2.3组态
win8/win8.1+vs2013+cocos2d-x-2.2.3组态 前言: 这是我人生第一篇博客,假设你认为不好,还请见谅!!!!! 工具: 1.vs2013 2.Python2.7(百度一大片 ...
- Fedora15下搭建QT开发环境及编译QT(提前一键安装完,qt编译所有必需库 yum install gcc-c++ libXtst-devel freetype freetype-devel fontconfig-devel libXrender-devel )
看了不少linux上编译qt的文章,实际上直接通过yum 安装qt是最方便的,请参考<yum安装qt> 不过初步接触fedora,为了了解一下如何在linux上编译.安装开源代码,所以必须 ...
- VISTA 与输入法程式介面
原文:VISTA 与输入法程式介面 VISTA 与输入法程式介面 文/黄忠成 近日,我所兼职顾问的公司开始将旧有的Win32 程式及新开发的.NET 应用程式移转到VISTA 系 ...
- 一步一步造个IoC轮子(一):IoC是什么
一步一步造个Ioc轮子目录 一步一步造个IoC轮子(一):IoC是什么 一步一步造个IoC轮子(二):详解泛型工厂 一步一步造个IoC轮子(三):构造基本的IoC容器 前言 .net core正式版前 ...
- murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法
murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法 1.DJB HASH算法 1 2 3 4 5 6 7 8 9 10 11 /* the famous DJB Hash ...
- 【转】编程之道 之 Rob Pike
1.你无法断定程序会在什么地方耗费运行时间.瓶颈经常出现在想不到的地方,所以别急于胡乱找个地方改代码,除非你已经证实那儿就是瓶颈所在. 2.估量.在你没对代码进行估量,特别是没找到最耗时的那部分之前, ...
- BooleanToColorConverter
public class BooleanToColorConverter : IValueConverter { public object Convert(object value, Type ta ...
- asp.net 调用带证书的webservice解决办法
最近在朋友弄一个调整省政府政务工作流的程序.. 需要把当前的信息推送到政务网上,采用的是带证书的https webservice.. 下面说一下实现过程 第一步,引用webservice地址,删除we ...
- Android疑难杂症之Theme
背景:最近在把自己之前写的一个应用换成Material Design风格,在看官方Guide后动手试了一试,没想到出门就遇到了坑,在换成Material Design风格的主题后,我设置了一下colo ...
- UWP入门(十一)--使用选取器打开文件和文件夹
原文:UWP入门(十一)--使用选取器打开文件和文件夹 很漂亮的功能,很有趣 重要的 API FileOpenPicker FolderPicker StorageFile 通过让用户与选取器交互来访 ...