poj 2251 三维地图最短路径问题 bfs算法
题意:给你一个三维地图,然后让你走出去,找到最短路径。
思路:bfs
- 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t
struct node
{
int x, y, z;
int t;
}; - bfs用队列,进队列的时候要标记,并且 t+1;
- 最先到达终点的,所花的时间必定最短
代码上的小技巧:
三维地图需要你去遍历的时候需要走六个方向:
int dx[] = { ,,,,,- };
int dy[] = { ,,-,,, };
int dz[] = { ,-,,,, };
解决问题的代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
int r, n, m;
string s[][];
struct node
{
int x, y, z;
int t;
};
node Begin, End;
queue <node> que;
int dx[] = { ,,,,,- };
int dy[] = { ,,-,,, };
int dz[] = { ,-,,,, };
int vis[][][];
int bfs()
{
que.push(Begin);
vis[Begin.x][Begin.y][Begin.z] = ;
while (!que.empty())
{
node front = que.front();
que.pop();
if (front.x == End.x && front.y == End.y && front.z == End.z) return front.t;
for (int i = ; i < ; i++)
{
node tmp;
tmp.x = front.x + dx[i];
tmp.y = front.y + dy[i];
tmp.z = front.z + dz[i];
tmp.t = front.t + ;
if (tmp.x >= && tmp.x < r && tmp.y >= && tmp.y < n && tmp.z >= && tmp.z < m && !vis[tmp.x][tmp.y][tmp.z] && s[tmp.x][tmp.y][tmp.z] != '#')
{
que.push(tmp);
vis[tmp.x][tmp.y][tmp.z] = ;
}
}
}
return -;
}
int main()
{
while (cin >> r >> n >> m)
{
if (r == && n == && m == ) break;
memset(vis, , sizeof(vis));
while (!que.empty())
{
que.pop();
}
for (int i = ; i < r; i++) {
string ret;
for (int j = ; j < n; j++) {
cin >> s[i][j];
for (int k = ; k < m; k++) {
if (s[i][j][k] == 'S') {
Begin.x = i, Begin.y = j, Begin.z = k; Begin.t = ;
}
else if (s[i][j][k] == 'E') {
End.x = i, End.y = j, End.z = k; End.t = ;
}
}
}
}
int ans = bfs();
if (ans == -) {
printf("Trapped!\n");
}
else {
printf("Escaped in %d minute(s).\n", ans);
}
} return ;
}
poj 2251 三维地图最短路径问题 bfs算法的更多相关文章
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- POJ 2251 三维BFS(基础题)
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- POJ 2251 Dungeon Master(三维空间bfs)
题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue& ...
- poj 2251 Dungeon Master 3维bfs(水水)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21230 Accepted: 8261 D ...
- poj 3669 火星撞地球问题 bfs算法
题意:火星撞地球,你要跑到一个永远安全的地方,求最短时间 思路:bfs+预处理 这题的数据量比较大,所以需要进行预处理 对每个位置设上时间(被撞的最早时间) 未被撞的设为-1 for (int j = ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ - 2251 Dungeon Master 【BFS】
题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...
随机推荐
- 修改Tomcat和Jetty默认JDK
tomcat: sed -i 's/java-7-oracle/java-8-oracle/g' /etc/init.d/tomcat7 Jetty echo 'JAVA_HOME=/usr/lib/ ...
- awk单引号处理
awk中使用单引号,常规字符串,'\''即可,但如果像下面在$4变量用单引号,则还需要加上双引号才行. cat 2.txt | awk '{ print $1, $2, $3, "'\''& ...
- P3930 SAC E#1 - 一道大水题 Knight
TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...
- Vue双向绑定简单实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue2.0:(九)、外卖App弹窗部分星星评分
本篇是星星评分部分,先上代码: 1.header.vue: <template> <transition name="fade"> & ...
- SPEC CPU 2006编译perl 出错:undefined reference to `pow'
问题来源: 编译spec2006时,出现如下错误: cc -L/home/yrtan/benchmark/2006/CPU2006v1.0.1/tools/output/lib -L/usr/loca ...
- linux命令之文本查看
vi掌握练习: 英文文档,相同的单词复制粘贴光标移动编辑等操作: cat:显示文件所有内容,小文件查看时使用. 缺点:文件大时不方便查看,文件很大时,会抢占系统资源,会出现命令崩溃. [zyj@loc ...
- SharePoint 2016 如何修改Library 地址
Scenario #1 如何为一个Library 修改下访问 网络路径地址 1.点击library,点开open with explorer,使用Windows资源管理器打开文档库 2.在文件夹层次结 ...
- 2406: C语言习题 求n阶勒让德多项式
2406: C语言习题 求n阶勒让德多项式 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 961 Solved: 570[Submit][Status ...
- Linux Cache 机制探究
http://www.penglixun.com/tech/system/linux_cache_discovery.html