题目 :
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
Is an escape possible? If yes, how long will it take?
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
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.
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
Sample Input
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!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
理解:就是一个正常的搜素,但是要注意的是我们要多开一维数组,也就是平时开的二维数组这个题开三维数组来解决这个问题,具体代码如下,不懂欢迎评论:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char Map[109][109][109];
int h , m , n ;
int dir[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; //方向
struct node{
int x , y , z , step;
}e,s,Now,Next;
bool flag(int z,int x , int y){
if(z >= 0 && z < h && x >= 0 && x < m && y >= 0 && y < n && Map[z][x][y] != '#')
return true ;
return false ;
} //判断的函数
int bfs(){
queue<node> Q ; //队列
Q.push(s); // 入队
while(Q.size()){ //队列内元素
Now = Q.front();
Q.pop(); //返回顶
if(Now.x == e.x && Now.y == e.y && Now.z == e.z)
return Now.step;
for(int i = 0 ; i < 6 ; i ++){
Next.x = Now.x + dir[i][0];
Next.y = Now.y + dir[i][1];
Next.z = Now.z + dir[i][2];
Next.step = Now.step + 1 ;
if(flag(Next.z,Next.x,Next.y) == 1){
Map[Next.z][Next.x][Next.y] = '#' ;
Q.push(Next);
}
}
}
return -1 ;
}
int main(){
while(scanf("%d%d%d",&h,&m,&n),h+m+n){
for(int i = 0 ; i < h ; i ++){
for(int j = 0 ; j < m ; j ++){
for(int k = 0 ; k < n ; k ++){
cin>>Map[i][j][k];
if(Map[i][j][k] == 'S'){
s.z = i ;
s.x = j ;
s.y = k ;
s.step = 0 ;
}
if(Map[i][j][k] == 'E'){
e.z = i ;
e.x = j ;
e.y = k ;
}
}
}
}
int ans = bfs();
if(ans != -1)
printf("Escaped in %d minute(s).\n", ans);
else
printf("Trapped!\n");
}
return 0 ;
}