题目链接:http://poj.org/problem?id=2251
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 51402 | Accepted: 19261 |
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.
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
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! 题意: 在一个三维空间中,# 表示石块,不能走,. 表示路,可以走,求从 S 到 E 花费的最短时间,每走一步花费时间加 1 。
注意:
1.不要写 if(bfs()) printf("%d",bfs()),这种形式,要写一个中间变量 int ans = bfs() ,然后,if(ans) printf("%d",ans);
2.这是一个三维空间,有六个方向
3.不要只判断当前位置是不是被标记过,还要判断当前位置是不是石头(“#”);
#include<iostream>
#include<queue>
#include <cstring>
#include<cstdio> using namespace std; const int maxn = ;
int go[][] = {,,,,,-,-,,,,,,,,,,-,};
bool mark[maxn][maxn][maxn];
char maze[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
int L,R,C;
struct node
{
int x,y,z;
int time;
}; bool Isok(int x,int y,int z)
{
//别忘了判断是不是'#'
return x >= && x < L && y >= && y < R && z >= && z < C && !mark[x][y][z] && maze[x][y][z] != '#';
}
int bfs()
{
queue<node> q;
struct node cu,ne;
cu.x=sx;cu.y=sy;cu.z=sz;
cu.time = ;
mark[sx][sy][sz]=;
q.push(cu);
while(!q.empty())
{
cu = q.front();
q.pop();
if(cu.x==ex&&cu.y==ey&&cu.z==ez)
return cu.time;
for(int i=;i<;i++)
{
ne.x = cu.x + go[i][];
ne.y = cu.y + go[i][];
ne.z = cu.z + go[i][];
if(Isok(ne.x,ne.y,ne.z))
{
mark[ne.x][ne.y][ne.z] = ;
ne.time = cu.time + ;
q.push(ne);
}
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C))
{
memset(mark,,sizeof(mark));
memset(maze,,sizeof(maze));
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
scanf("%s",maze[i][j]);
getchar(); //有没有getchar()都能过
}
for(int i=;i<L;i++)
{
for(int j=;j<R;j++)
{
for(int k=;k<C;k++)
{
if(maze[i][j][k] == 'S')
{
sx=i;sy=j;sz=k;
}
else if(maze[i][j][k] == 'E')
{
ex=i;ey=j;ez=k;
}
}
}
}
//不要直接使用bfs()的返回值做判断和做printf输出变量。
int ans = bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
Ac代码