标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27520 | Accepted: 10776 |
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
1 #include "iostream" 2 #include "cstdio" 3 #include "fstream" 4 #include "sstream" 5 #include "cstring" 6 7 using namespace std; 8 const int maxN = 3e4 + 1e2 ; 9 struct Queue { int x , y , z ; } ; 10 typedef long long QAQ ; 11 12 int dx[ ] = { 1 , -1 , 0 , 0 , 0 , 0 } ; 13 int dy[ ] = { 0 , 0 , 0 , 0 , -1 , 1 } ; 14 int dz[ ] = { 0 , 0 , -1 , 1 , 0 , 0 } ; 15 16 int step[ maxN ] ; 17 Queue Q[ maxN ] ; 18 bool vis[ 40 ][ 40 ][ 40 ] ; 19 char map[ 40 ][ 40 ][ 40 ] ; 20 21 int L , R , C ; 22 int start_x , start_y , start_z , des_x , des_y , des_z ; 23 //QAQ Ans ; 24 25 void Init ( ) { 26 memset ( vis , false , sizeof ( vis ) ) ; 27 memset ( step , 0 , sizeof ( step ) ) ; 28 } 29 30 inline bool Check ( const int x_x , const int y_y , const int z_z ) {return ( x_x == des_x && y_y == des_y && z_z == des_z ) ? true : false ; } 31 int BFS ( ) { 32 int Head = 0 , Tail = 0 ; 33 Q[ Tail ].x = start_x ; 34 Q[ Tail ].y = start_y ; 35 Q[ Tail ].z = start_z ; 36 while ( Head <= Tail ) { 37 for ( int i=0 ; i<6 ; ++i ) { 38 int xx = Q[ Head ].x + dx[ i ] ; 39 int yy = Q[ Head ].y + dy[ i ] ; 40 int zz = Q[ Head ].z + dz[ i ] ; 41 if( !vis[ xx ][ yy ][ zz ] && ( map[ xx ][ yy ][ zz ] == ‘.‘ ) && xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && zz < C ) { 42 vis[ xx ][ yy ][ zz ] = true ; 43 Q[ ++Tail ].x = xx ; 44 Q[ Tail ].y = yy ; 45 Q[ Tail ].z = zz ; 46 step [ Tail ] = step[ Head ] + 1 ; 47 if ( Check ( xx , yy , zz ) ) return step[ Tail ] ; 48 } 49 } 50 ++ Head ; 51 } 52 return false ; 53 } 54 55 void Scan ( ) { 56 for ( int i=0 ; i<L ; ++i , getchar ( ) ){ 57 for ( int j=0 ; j<R ; ++j , getchar ( ) ){ 58 for ( int k=0 ; k<C ; ++k ) { 59 map[ i ][ j ][ k ] = getchar ( ) ; 60 if ( map[ i ][ j ][ k ] == ‘S‘ ) { 61 start_x = i ; 62 start_y = j ; 63 start_z = k ; 64 } 65 else if ( map[ i ][ j ][ k ] == ‘E‘ ) { 66 map[ i ][ j ][ k ] = ‘.‘ ; 67 des_x = i ; 68 des_y = j ; 69 des_z = k ; 70 } 71 } 72 } 73 } 74 } 75 76 inline void Print ( int temp ) { 77 if( temp ) printf ( "Escaped in %d minute(s).\n" , temp ) ; 78 else printf ( "Trapped!\n" ) ; 79 } 80 81 int main ( ) { 82 while ( scanf ( "%d %d %d\n " , &L , &R , &C ) == 3 && L && R && C ) { 83 Init ( ) ; 84 Scan ( ); 85 Print ( BFS( ) ) ; 86 } 87 return 0 ; 88 }
2016-10-19 18:50:40
(完)
标签:
原文地址:http://www.cnblogs.com/shadowland/p/5978379.html