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 ...
随机推荐
- abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十五)
core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+e ...
- IntelliJ IDEA 2019 快捷键终极大全,速度收藏!
转载注明:https://blog.csdn.net/WantFlyDaCheng/article/details/100078777 自动代码 查询快捷键 其他快捷键 调试快捷键 重构 十大Inte ...
- HDU 6040
题意略. 思路:题目就是在询问你m次,第k小是哪个数.首先我们可以想到直接排序后,即可O(1)来查找询问.但是题目中n的范围给的是1e7, 无法承受nlogn的复杂度.从而想到另外一种求静态第k小的方 ...
- macbook 安装redis流程及问题总结
Mac安装redis流程和总结 一.redis安装流程: 1.进入redis官网-->点击download-->选择稳定版本(stable)-->点击Download即可. 2.将下 ...
- 使用ansible对思科交换机备份
先决条件 - 了解ansible基本操作 - 了解网络设备相关操作 - 了解linux相关操作 安装 安装EPEL yum install https://dl.fedoraproject.org/p ...
- Jedis操作Redis--List类型
/** * List(列表) * BLPOP,BRPOP,BRPOPLPUSH,LINDEX,LINSERT,LLEN,LPOP,LPUSH,LPUSHX,LRANGE,LREM,LSET,LTRIM ...
- CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- Python 之父的解析器系列之五:左递归 PEG 语法
原题 | Left-recursive PEG grammars 作者 | Guido van Rossum(Python之父) 译者 | 豌豆花下猫("Python猫"公众号作者 ...
- NOIP 2016 组合数问题 题解
一道sb题目,注意范围,可打表解决,打出杨辉三角,在用前缀和求解即可 代码(一维前缀和) #include<bits/stdc++.h> using namespace std; int ...
- 除了FastJson,你还有选择: Gson简易指南
前言 这个周末被几个技术博主的同一篇公众号文章 fastjson又被发现漏洞,这次危害可导致服务瘫痪! 刷屏,离之前漏洞事件没多久,fastjson 又出现严重 Bug.目前项目中不少使用了 fast ...