Dungeon Master POJ-2251 三维BFS
题目链接:http://poj.org/problem?id=2251
题目大意
你被困在了一个三维的迷宫,找出能通往出口的最短时间。如果走不到出口,输出被困。
思路
由于要找最短路径,其实就是BFS。一般的BFS是前后左右四个方向,这个题相当于是变成能往上下左右前后六个方向找。修改一下二维BFS搜索部分的代码即可。
题解
#include <iostream>
#include <queue>
#include<fstream>
#include <cstring>
//#define debug
using namespace std; char a[][][];
int vis[][][];
int x,y,z; //范围 int d1[] = {,-,,,,};
int d2[] = {,,,-,,};
int d3[] = {,,,,,-}; struct point{
int x;
int y;
int z;
int step;
}p1,p2,e; bool isValid(point p){
if(p.x>= && p.x<x && p.y>= && p.y<y && p.z>= && p.z<z && (a[p.x][p.y][p.z] == '.' || a[p.x][p.y][p.z] == 'E' )&& vis[p.x][p.y][p.z] == ){
return true;
}
return false;
} bool success(point p){
if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
return true;
}
return false;
} void bfs(){
point tmp;
queue<point> q;
q.push(p1);
while(!q.empty()){
p2 = q.front();
q.pop();
if(success(p2)){
return;
}else{
for(int ii=;ii<;ii++){ //向六个方向搜索
tmp.x = p2.x+d1[ii];
tmp.y = p2.y+d2[ii];
tmp.z = p2.z+d3[ii];
if(isValid(tmp)){
tmp.step = p2.step+;
vis[tmp.x][tmp.y][tmp.z]= ;
q.push(tmp);
}
}
}
}
} int main()
{
#ifdef debug
//cin重定向
ifstream cin("C:\\Users\\Administrator\\Desktop\\test.txt");
#endif while((cin >> x >> y >> z)){
if(x == ){
break;
}
for(int i = ;i < x;i++){ //读入maze
for(int j = ;j < y;j++){
for(int k = ;k < z;k++){
cin >> a[i][j][k];
if(a[i][j][k] == 'S'){
p1.x = i;
p1.y = j;
p1.z = k;
p1.step = ;
}else if(a[i][j][k] == 'E'){
e.x = i;
e.y = j;
e.z = k;
}
}
}
} memset(vis,,sizeof(vis)); //初始化vis bfs();
if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
cout << "Escaped in " << p2.step << " minute(s)." << endl;
}else{
cout << "Trapped!" << endl;
}
}
}
Dungeon Master POJ-2251 三维BFS的更多相关文章
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- POJ 2251 三维BFS(基础题)
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- Dungeon Master POJ - 2251 (搜索)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48605 Accepted: 18339 ...
- 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 ...
- Dungeon Master POJ - 2251(bfs)
对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...
- B - Dungeon Master POJ - 2251
//纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
随机推荐
- 【转】Python实现智能五子棋
前言 棋需要一步一步下,人生需要一步一步走.千里之行,始于足下,九层之台,起于累土. 用Python五子棋小游戏. 基本环境配置 版本:Python3 相关模块: 本文所做工作如下: (1) 五子棋界 ...
- Python机器学习笔记:不得不了解的机器学习知识点(2)
之前一篇笔记: Python机器学习笔记:不得不了解的机器学习知识点(1) 1,什么样的资料集不适合用深度学习? 数据集太小,数据样本不足时,深度学习相对其它机器学习算法,没有明显优势. 数据集没有局 ...
- C++ socket bind()函数报错 不存在从 "std::_Binder<std::_Unforced, SOCKET &, sockaddr *&, size_t &>" 到 "int" 的适当转换函数
昨天还可以正常运行的程序,怎么今天改了程序的结构就报错了呢?我明明没有改动函数内部啊!!! 内心无数只“草泥马”在奔腾,这可咋办呢?于是乎,小寅开始求助于亲爱的度娘...... 由于小寅知识水平有限, ...
- spring boot环境配置
Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...
- ElementUI使用v-if控制tab标签显示遇到的Duplicate keys detected: 'xxx'问题
今天工作遇到一个问题: 需求背景:页面中有几个tab,需要根据登录用户的权限控制tab标签的显示与隐藏 . <el-tabs @tab-click="handleClick" ...
- spring-cloud-kubernetes与SpringCloud Gateway
本文是<spring-cloud-kubernetes实战系列>的第五篇,主要内容是在kubernetes上部署一个SpringCloud Gateway应用,该应用使用了spring-c ...
- 第10章 文档对象模型DOM 10.2 Document类型
Document 类型 JavaScript 通过 Document 类型表示文档.在浏览器中, document 对象是 HTMLDocument (继承自 Document 类型)的一个实例,表示 ...
- NLP(九) 文本相似度问题
多个维度判别文本之间相似度 情感维度 Sentiment/Emotion 感官维度 Sense 特定词的出现 词频 TF 逆文本频率 IDF 构建N个M维向量,N是文档总数,M是所有文档的去重词汇量 ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- CodeForces 779D. String Game(二分答案)
题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...