Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
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 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!
Source
Ulm Local 1997
题意:给定L个矩阵 , 每个Row行,Col列 , 求从S到E的最短步数,若无则输出Trapped!。
思路:3维BFS即可
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
int l , row , col ; //l:矩阵个数
int next[6][3] = {{0,0,1}, {0,0,-1} , {0,1,0} , {0,-1,0} , {1,0,0} ,{-1,0,0}} ;
// 方向 右,左,上,下,上个矩阵,下个矩阵 (3维)
char ch[32][32][32] ; //保存图
int ans[32][32][32] ; //记录步数
struct point {
int i , j , k ; //从上至下代表第i个矩阵 ; 第j行 ; 第k列
};
bool judge( point temp) {
if( temp.i < 0 || temp.i >= l )
return false ;
if( temp.j <0 || temp.j >= row )
return false ;
if( temp.k <0 || temp.k >= col )
return false ;
return true ;
}
int bfs( point start ){
int flag = false , bj = false ;
queue<point> Q ;
point temp ;
point init ;
while(!Q.empty() ) {
Q.pop() ;
}
Q.push(start) ;
ch[start.i][start.j][start.k] = '#' ;
while(!Q.empty()) {
temp = Q.front() ;
Q.pop() ;
for( int ll = 0 ; ll < 6 ; ++ll ) { //搜索下一步
init.i = temp.i+next[ll][0] ;
init.j = temp.j+next[ll][1] ;
init.k = temp.k+next[ll][2] ;
if( judge(temp)) { //判断边界
if( ch[init.i][init.j][init.k] == '.' ){
ch[init.i][init.j][init.k] = '#' ;
Q.push(init) ;
ans[init.i][init.j][init.k]=ans[temp.i][temp.j][temp.k] +1 ;
}
else if( ch[init.i][init.j][init.k] == 'E' ) {
ans[init.i][init.j][init.k]=ans[temp.i][temp.j][temp.k] +1 ;
return ans[init.i][init.j][init.k] ;
}
}
}
}
return false ;
}
int main(){
while(~scanf("%d%d%d",&l ,&row ,&col)&&(l||row||col)) {
memset(ch,0,sizeof(ch));
memset(ans,0,sizeof(ans)) ;
point st ;
for( int i = 0 ; i < l ; ++i ){
for( int j = 0 ; j < row ; ++j ) {
scanf("%s",ch[i][j] ) ;
for( int k = 0 ; k < col ; ++k) {
if( ch[i][j][k] == 'S') {
st.i = i ;
st.j = j ;
st.k = k ;
}
}
}
}
int step ;
if( step = bfs(st)) {
printf("Escaped in %d minute(s).\n", step ) ;
}
else printf("Trapped!\n") ;
}
return 0 ;
}