Poj2251 BFS模板题
Posted dreaminpal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Poj2251 BFS模板题相关的知识,希望对你有一定的参考价值。
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 62948 | Accepted: 23020 |
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?
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.
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 form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Escaped 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.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
代码一(STL queue)
#include<cstdio> #include<queue> #include<cstring> using namespace std; struct node int x, y, z; int step; S, Node; const int maxn = 35; int X[6] = 1,-1,0,0,0,0; int Y[6] = 0,0,1,-1,0,0; int Z[6] = 0,0,0,0,1,-1; int n, r, c; int ans; bool inq[maxn][maxn][maxn]; char maze[maxn][maxn][maxn]; bool judge(int z, int y, int x) if(z < 0 || z >= n || y < 0 || y >= r || x < 0 || x >= c || inq[z][y][x] == true|| maze[z][y][x] == ‘#‘) return false; return true; bool BFS() queue<node>q; q.push(S); while(!q.empty()) node top = q.front(); q.pop(); if(maze[top.z][top.y][top.x] == ‘E‘) if(top.step == 0) return false; else ans = top.step; return true; int ii; for(ii = 0; ii < 6; ii++) int newX = top.x + X[ii]; int newY = top.y + Y[ii]; int newZ = top.z + Z[ii]; if(judge(newZ, newY, newX)) Node.x = newX; Node.y = newY; Node.z = newZ; Node.step = top.step+1; q.push(Node); inq[newZ][newY][newX] = true; return false; int main() while(scanf("%d%d%d", &n, &r, &c) && n && r && c) memset(inq, false, sizeof(inq)); int i, j, k; for(i = 0; i < n; i++) for(j = 0; j < r; j++) scanf("%s", maze[i][j]); for(k = 0; k < c; k++) if(maze[i][j][k] == ‘S‘) S.x = k; S.y = j; S.z = i; bool p = BFS(); if(!p) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", ans); return 0;
以上是关于Poj2251 BFS模板题的主要内容,如果未能解决你的问题,请参考以下文章