B - Dungeon Master POJ - 2251

 1 //纯bfs
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <queue>
 7
 8 using namespace std;
 9 const int maxn = 33;
10 char g[maxn][maxn][maxn];
11 bool vis[maxn][maxn][maxn];
12 int f[6][3] = { { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } };
13 int L, R, C;
14 struct node{
15     int x, y, z;
16     int flag;
17 }last, now;
18
19 void bfs(int x1, int y1, int z1){
20     vis[z1][x1][y1] = true;
21     queue<node>q;
22     while (!q.empty()){
23         q.pop();
24     }
25     last.x = x1; last.y = y1; last.z = z1;
26     last.flag = 0;
27     q.push(last);
28     while (!q.empty()){
29         last = q.front();
30         q.pop();
31         if (g[last.z][last.x][last.y] == ‘E‘){
32             cout << "Escaped in " << last.flag << " minute(s)." << endl;
33             return;
34         }
35         for (int i = 0; i < 6; i++){
36             now.z = last.z + f[i][0];
37             now.x = last.x + f[i][1];
38             now.y = last.y + f[i][2];
39             now.flag = last.flag + 1;
40             if (now.z < 0 || now.z >= L || now.x < 0 || now.y < 0 || now.x >= R || now.y >= C)
41                 continue;
42             if (!vis[now.z][now.x][now.y] && g[now.z][now.x][now.y] != ‘#‘){
43                 vis[now.z][now.x][now.y] = true;
44                 q.push(now);
45             }
46         }
47     }
48     cout << "Trapped!" << endl;
49 }
50
51 int main(){
52     ios::sync_with_stdio(false);
53     while (cin>>L>>R>>C){
54         if (!L && !R && !C)
55             break;
56         int x, y, z;
57         memset(vis, false, sizeof(vis));
58         for (int i = 0; i < L; i++){
59             for (int j = 0; j < R; j++){
60                 for (int k = 0; k < C; k++){
61                     char s;
62                     cin >> s;
63                     g[i][j][k] = s;
64                     if (s == ‘S‘){
65                         z = i; x = j; y = k;
66                     }
67                 }
68             }
69         }
70         bfs(x, y, z);
71
72     }
73     return 0;
74 }

原文地址:https://www.cnblogs.com/jaydenouyang/p/8719002.html

时间: 2024-10-14 16:00:48

B - Dungeon Master POJ - 2251的相关文章

Dungeon Master poj 2251 dfs

Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855   Accepted: 6564 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or

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

链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21370   Accepted: 8299 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may

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 unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled wit

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled