Dungeon Master(三维bfs)
生活随笔
收集整理的這篇文章主要介紹了
Dungeon Master(三维bfs)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:http://poj.org/problem?id=2251
題目:
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 with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the formEscaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.###### ##### ##.## ##...##### ##### #.### ####E1 3 3 S## #E# ###0 0 0Sample Output
Escaped in 11 minute(s). Trapped!題意:你處在一個(gè)三維的地牢里,從S出發(fā)逃到出口E,問(wèn)最少要跑多遠(yuǎn)。
思路:這題雖然是一個(gè)三維的地圖,但是做法和二維的沒(méi)多大區(qū)別,不過(guò)要從當(dāng)前層到其他層的要求是你所在位置為非#,且你將到的那層的這個(gè)位置也是非#。
代碼實(shí)現(xiàn)如下: 1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int inf = 0x3f3f3f3f; 7 int l, r, c, ans; 8 int sx, sy, sz; 9 char mp[35][35][35]; 10 int vis[35][35][35]; 11 12 struct node { 13 int x, y, z, step; 14 }nw, nxt; 15 16 int dx[6] = {1, -1, 0, 0, 0, 0}, dy[6] = {0, 0, 1, -1, 0, 0}, 17 dz[6] = {0, 0, 0, 0, 1, -1}; 18 19 void bfs(int z, int x, int y) { 20 vis[z][x][y] = 1; 21 nw.z = z, nw.x = x, nw.y = y, nw.step = 0; 22 queue<node> q; 23 q.push(nw); 24 while(!q.empty()) { 25 nw = q.front(), q.pop(); 26 if(mp[nw.z][nw.x][nw.y] == 'E') { 27 ans = nw.step; 28 return; 29 } 30 for(int i = 0; i < 6; i++) { 31 nxt.z = nw.z + dz[i]; 32 nxt.x = nw.x + dx[i]; 33 nxt.y = nw.y + dy[i]; 34 if(nxt.z >= 0 && nxt.z < l && nxt.x >= 0 && nxt.x < r && nxt.y >=0 && nxt.y < c && vis[nxt.z][nxt.x][nxt.y] == 0 && mp[nxt.z][nxt.x][nxt.y] != '#') { 35 nxt.step = nw.step + 1; 36 vis[nxt.z][nxt.x][nxt.y] = 1; 37 q.push(nxt); 38 } 39 } 40 } 41 } 42 43 int main() { 44 while(~scanf("%d%d%d", &l, &r, &c) && (l + r + c)) { 45 for(int i = 0; i < l; i++) { 46 for(int j = 0; j < r; j++) { 47 scanf("%s", mp[i][j]); 48 for(int k = 0; k < c; k++) { 49 if(mp[i][j][k] == 'S') { 50 sx = j, sy = k, sz =i; 51 } 52 } 53 } 54 } 55 memset(vis, 0, sizeof(vis)); 56 ans = inf; 57 bfs(sz, sx, sy); 58 if(ans >= inf) { 59 printf("Trapped!\n"); 60 } else { 61 printf("Escaped in %d minute(s).\n", ans); 62 } 63 } 64 return 0; 65 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/Dillonh/p/8974741.html
總結(jié)
以上是生活随笔為你收集整理的Dungeon Master(三维bfs)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [性能测试]:关于MQ协议脚本开发
- 下一篇: JMS学习七(ActiveMQ之Topi