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 ...
随机推荐
- wpf无边框窗体移动和大小调整
原文:wpf无边框窗体移动和大小调整 using System; using System.Windows; using System.Windows.Interop; namespace Wpf ...
- 机器学习:深入理解 LSTM 网络 (一)
Recurrent Neural Network Long Short Term Memory Networks (LSTMs) 最近获得越来越多的关注,与传统的前向神经网络 (feedforward ...
- ATS项目更新(2) 命令行编译Studio解决方案
1: rem "D:\Microsoft Visual Studio 8\SDK\v2.0\Bin\sdkvars.bat" 2: D: 3: cd ..\..\..\..\..\ ...
- WPF 用Clip属性实现蒙板特效
原文:WPF 用Clip属性实现蒙板特效 上一篇,已简单介绍Clip属性的用法,这一篇用它来实现简单蒙板功能,很简单,直接上代码 <Window x:Class="擦除效果.MainW ...
- 标准差standard deviation和标准错误standard error你能解释一下
by:ysuncn(欢迎转载,请注明原创信息) 什么是标准差(standard deviation)呢?依据国际标准化组织(ISO)的定义:标准差σ是方差σ2的正平方根:而方差是随机变量期望的二次偏差 ...
- 网络库Asio交叉编译(Linux生成ARM)
1. Asio是一个跨平台的C++库,用于网络和底层I/O编程.Asio使用先进的C++方式提供了一系列的异步模型 2. 官方网址:http://think-async.com 3. 由于Asio库 ...
- 不得不说,我太佩服node了,连openXML也搞定了!
https://github.com/Ziv-Barber/officegen 开源项目地址 使用报告等有空完成!
- WPF 通过CommandBinding捕获命令
RoutedCommand与业务逻辑无关,业务逻辑是通过CommandBinding来实现 using System; using System.Collections.Generic;using S ...
- 插入2D点,在WPF中使用Bezier曲线
原文Interpolate 2D points, usign Bezier curves in WPF Interpolate 2D points, usign Bezier curves in WP ...
- Linux学习之“vfork函数”
为什么使用vfork()? 希望父子进程执行不同的代码.例如: 网络服务程序中,父进程等待客户端的服务请求,当请求达到时,父进程调用fork,使子进程处理该次请求,而父进程继续等待下一个服务请求到达. ...