就是一道简单的三维bfs,题意很明确,可是我居然WA了三次,都无奈了,最后才发现是原来是用的队列没有清空,无奈了,直接把队列的生命放在bfs函数里
无奈,无奈,无奈。。。。。
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29488 | Accepted: 11466 |
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!
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
int inf = 0x3f3f3f3f;
const int maxn = 35;
int l, r, c;
int dir[6][3] = { {0, 0, -1}, {0, -1, 0}, {0, 0, 1}, {0, 1, 0}, {-1, 0, 0}, {1, 0, 0}};
char maze[maxn][maxn][maxn];
int book[maxn][maxn][maxn];
struct node {
int x, y, z;
int step;
};
int bfs(int sx, int sy, int sz) {
queue<node> road; //定义在bfs函数里面就不需要考虑清空队列的的问题了
memset(book, 0, sizeof(book));
node now, next;
now.x = sx, now.y = sy, now.z = sz, now.step = 0;
road.push(now);
book[sz][sx][sy] = 1;
while(!road.empty()) {
now = road.front();
road.pop();
int i;
for(i=0; i<6; ++i) {
next.z = now.z + dir[i][0];
next.x = now.x + dir[i][1];
next.y = now.y + dir[i][2];
next.step = now.step + 1;
if(next.x <0 || next.x >= r || next.y < 0 || next.y >=c || next.z < 0 || next.z >= l)
continue;
if(book[next.z][next.x][next.y] || maze[next.z][next.x][next.y] == '#')
continue;
if(maze[next.z][next.x][next.y] == 'E')
return next.step;
book[next.z][next.x][next.y] = 1;
road.push(next);
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d", &l, &r, &c) && l && r && c) {
int i, j, k, sx, sy, sz;
for(i=0; i<l; ++i) {
for(j=0; j<r; ++j) {
scanf("%s", maze[i][j]);
for(k=0; k<c; ++k) {
if(maze[i][j][k] == 'S')
sx = j, sy = k, sz = i;
}
}
}
int temp = bfs(sx, sy, sz);
if(temp != -1)
printf("Escaped in %d minute(s).\n", temp);
else
printf("Trapped!\n");
}
return 0;
}