

POJ2251 Dungeon Master(BFS)
source link: https://arminli.com/poj2251/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

POJ2251 Dungeon Master(BFS)
January 15, 2016
题意:三维的图,可以上下东南西北的走,所以方向是 6 个。在同坐标的不同 level 可以通过上下到达。每步时间是 1,问从 S 到 E 的最短时间。
#include<iostream> #include<cmath> #include<queue> #include<cstring> #include<string> #include<cstdio> #include<algorithm> using namespace std; int dir[6][3]={-1,0,0, 1,0,0, 0,-1,0, 0,1,0, 0,0,-1, 0,0,1}; int l, r, c, ans; char pic[35][35][35]; int flag[35][35][35]; struct point{ int x, y, z; int n; }; bool judge(int x, int y, int z){ if(x >= 0 && x <l && y >= 0 && y <r && z >= 0 && z < c) return 1; else return 0; } void bfs(int x, int y, int z){ queue<point> q; point p0, p1, p2; p0.x = x; p0.y = y; p0.z = z; p0.n = 0; flag[x][y][z] = 1; q.push(p0); while(!q.empty()){ p1 = q.front(); q.pop(); for(int i = 0; i < 6; i++){ int x1 = p1.x + dir[i][0]; int y1 = p1.y + dir[i][1]; int z1 = p1.z + dir[i][2]; if(judge(x1, y1, z1) && !flag[x1][y1][z1] && pic[x1][y1][z1] != '#'){ if(pic[x1][y1][z1] == 'E'){ ans = p1.n + 1; return; } p2.x = x1; p2.y = y1; p2.z = z1; p2.n = p1.n+1; flag[x1][y1][z1] = 1; q.push(p2); } } } } int main(){ //freopen("a.txt", "r", stdin); int si, sj, sk; while(scanf("%d %d %d", &l, &r, &c) != EOF){ if(!l && !r && !c) break; memset(flag, 0, sizeof(flag)); for(int i = 0; i < l; i++){ for(int j = 0; j < r; j++){ scanf("%s", pic[i][j]); for(int k = 0; k < c; k++) if(pic[i][j][k] == 'S'){ si = i; sj = j; sk = k; } } } ans = -1; bfs(si, sj, sk); if(ans != -1) printf("Escaped in %d minute(s).n", ans); else printf("Trapped!n"); } return 0; }
Recommend
-
17
前言 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath First Search)是图论中两种非常重要的算法,生产上广泛用于拓扑排序,寻路(走迷宫),搜索引擎,爬虫等,也频繁出现在 leetcode...
-
10
The BFS Graph Traversal Algorithm in Java Learn how to impl...
-
13
Dec 11 2018Breadth First Search(BFS) in C# Hello Friends, In this article I will discuss one of the two traversing mechanisms used for tree and gr...
-
11
BFS 算法框架套路详解 👆让天下没有难刷的算法!若 GitBook 访问太慢,可尝试
-
15
BFS 算法秒杀各种益智游戏 👆让天下没有难刷的算法!若
-
5
译者:AI研习社( 季一帆 ) 双语原文链接: Interactive pathfinding 点此...
-
4
Dungeon Master POJ 2251 - Dungeon Master Time: 1000MS Memory: 65536K 难度: 初级 分类: BF...
-
6
Spielworks’ ‘Wombat Dungeon Master’ Dominates 8th Season with Massive Growth and NFT Minting March 30, 2023
-
7
Startups Becoming a Dungeon Master for an Interview
-
11
February 16, 2024Google Gemini as Your Dungeon Master
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK