Advertisement
NonWhite

H -day 2

Jul 24th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <stack>
  11. #include <sstream>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <cctype>
  15. #define clr( x , y ) memset( x ,y , sizeof x )
  16. #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  17. #define mp make_pair
  18. #define TAM 210
  19. #define debug( x ) cout << #x << " = " << x << endl
  20. #define f(i,n) for(int i = 0 ; i < n ; i++)
  21. #define ff(i,a,b) for(int i = a ; i < b ; i++)
  22. #define all( x ) x.begin() , x.end()
  23. #define ral( x ) x.rbegin() , x.rend()
  24. #define EPS 1E-9
  25.  
  26. using namespace std ;
  27.  
  28. typedef pair<int,int> ii ;
  29. typedef pair<ii,int> pii ;
  30. typedef long long ll ;
  31. typedef long double ld ;
  32.  
  33. int dx[] = {-1,1,0,0} ;
  34. int dy[] = {0,0,-1,1} ;
  35.  
  36. string g[ TAM ] ;
  37. ii dist[ TAM ][ TAM ] ;
  38. int n , m ;
  39. int vert , hor ;
  40. double D ;
  41.  
  42. ii bfs( int iniX , int iniY ){
  43.     queue<ii> q ;
  44. //  clr( dist , 0 ) ;
  45.     for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) dist[ i ][ j ] = mp( -1,0) ;
  46.     q.push( mp( iniX , iniY ) ) ;
  47.     dist[ iniX ][ iniY ] = mp( 0 , 0 ) ;
  48.  
  49.     while( !q.empty() ){
  50.         ii node = q.front() ; q.pop() ;
  51.         int x = node.first ;
  52.         int y = node.second ;
  53.         if( g[ x ][ y ] == 'E' ) return dist[ x ][ y ] ;
  54.        
  55.  
  56.         int dd2 = dist[ x ][ y ].first + dist[ x ][ y ].second ;
  57.         /* vert */
  58.         for(int i = 0 ; i < 2 ; i++){
  59.             int nx = x + dx[ i ] ;
  60.             int ny = y + dy[ i ] ;
  61.             if( nx < 0 || ny < 0 || nx >= n || ny >= m ) continue ;
  62.             if( g[ nx ][ ny ] == '#' ) continue ;
  63.             int DX = dist[ nx ][ ny ].first ;
  64.             int DY = dist[ nx ][ ny ].second ;
  65.             int dd1 = DX + DY ;
  66.             if( dd1 > dd2 + 1 || dd1 == -1 ){
  67.                 dist[ nx ][ ny ] = mp( dist[ x ][ y ].first + 1 , dist[ x ][ y ].second ) ;
  68.                 q.push( mp( nx , ny ) ) ;
  69.             }
  70.         }  
  71.         /* hor */
  72.         for(int i = 2 ; i < 4 ; i++){
  73.             int nx = x + dx[ i ] ;
  74.             int ny = y + dy[ i ] ;
  75.             if( nx < 0 || ny < 0 || nx >= n || ny >= m ) continue ;
  76.             if( g[ nx ][ ny ] == '#' ) continue ;
  77.             int DX = dist[ nx ][ ny ].first ;
  78.             int DY = dist[ nx ][ ny ].second ;
  79.             int dd1 = DX + DY ;
  80.             if( dd1 > dd2 + 1 || dd1 == -1 ){
  81.                 q.push( mp( nx , ny ) ) ;
  82.                 dist[ nx ][ ny ] = mp( dist[ x ][ y ].first , dist[ x ][ y ].second + 1 ) ;
  83.             }
  84.         }
  85.     }
  86.     return mp( -1, -1 ) ;
  87. }
  88.  
  89. double F( double peso , double vert ){
  90.     return peso * vert + hor * 1.0 ;
  91. }
  92.  
  93. double binary(){
  94.     double lt = 0 , rt = 10 ;
  95.    
  96.     while( fabs( rt -lt)>EPS ){
  97.         double mid = ( rt + lt ) / 2.0 ;
  98.        
  99.         if( F( mid , vert ) <D )
  100.             lt = mid ;
  101.         else
  102.             rt = mid ;
  103.     }
  104.     return lt ;
  105. }
  106.  
  107. int main(){
  108.  
  109.     int test ;
  110.     scanf("%d" , &test ) ;
  111.     for(int k = 0 ; k < test ; k++){
  112.         scanf("%lf%d" , &D , &n ) ;
  113. //      debug( D ) ;
  114. //      debug( n ) ;
  115.         cin.ignore() ;
  116.         for(int i = 0 ; i < n ; i++) getline( cin , g[ i ] ) ;
  117.         m = g[ 0 ].size() ;
  118.         int x , y ;
  119. //      debug(n),debug(m);
  120.         for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) if( g[ i ][ j ] == 'S' ) x = i , y = j ;
  121. //      debug(x),debug(y); 
  122.         ii L = bfs( x , y ) ;
  123.         vert = L.first ;
  124.         hor = L.second ;
  125.         debug( vert ) ;
  126.         debug( hor ) ;
  127.         printf("Case #%d: %.3lf\n" , k + 1 , binary()*100.0 ) ;
  128.     }
  129.     return 0 ;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement