题目链接: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的更多相关文章

  1. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

  2. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

  3. POJ 2251 三维BFS(基础题)

    Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  4. Dungeon Master POJ - 2251 (搜索)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48605   Accepted: 18339 ...

  5. 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 ...

  6. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...

  7. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  8. kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251

    题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...

  9. B - Dungeon Master POJ - 2251

    //纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...

  10. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

随机推荐

  1. Liunx学习总结(六)--进程

    进程概述 简单来讲程序是一个包含可以执行代码的静态的文件.进程是一个开始执行但是还没有结束的程序的实例.当程序被系统调用到内存以后,系统会给程序分配一定的资源(内存,设备等等)然后进行一系列的复杂操作 ...

  2. CentOS重置MySQL root密码的方法

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] skip-grant-tables  ...

  3. JavaScript Array 数组方法汇总

    JavaScript Array 数组方法汇总 1. arr.push() 从后面添加元素,返回值为添加完后的数组的长度 var arr = [1,2,3,4,5] console.log(arr.p ...

  4. Mysql高手系列 - 第4天:DDL常见操作汇总

    这是Mysql系列第4篇. 环境:mysql5.7.25,cmd命令中进行演示. DDL:Data Define Language数据定义语言,主要用来对数据库.表进行一些管理操作. 如:建库.删库. ...

  5. Python数据类型详解——字典

    Python数据类型详解--字典 引子 已经学习了列表,现在有个需求--把公司每个员工的姓名.年龄.职务.工资存到列表里,你怎么存? staff_list = [ ["Kwan", ...

  6. 【基准测试】JMH 简单入门

    JMH 简单入门 什么是 JMH JMH 是 Java Microbenchmark Harness 的缩写.中文意思大致是 "JAVA 微基准测试套件".首先先明白什么是&quo ...

  7. codeforces 456 E. Civilization(并查集+数的直径)

    题目链接:http://codeforces.com/contest/456/problem/E 题意:给出N个点,M条边,组成无环图(树),给出Q个操作,操作有两种: 1 x,输出x所在的联通块的最 ...

  8. codeforces 456 D. A Lot of Games(字典数+博弈+思维+树形dp)

    题目链接:http://codeforces.com/contest/456/problem/D 题意:给n个字符串.进行k次游戏.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为 ...

  9. codeforces 789 C. Functions again(dp求区间和最大)

    题目链接:http://codeforces.com/contest/789/problem/C 题意:就是给出一个公式 然后给出一串数求一个区间使得f(l,r)最大. 这题需要一个小小的处理 可以设 ...

  10. PHP 实现字符串表达式计算

    什么是字符串表达式?即,将我们常见的表达式文本写到了字符串中,如:"$age >= 20",$age 的值是动态的整型变量. 什么是字符串表达式计算?即,我们需要一段程序来执 ...