bzoj1193
1 #include<cstdio> 2 #include<cstdlib> 3 #include<algorithm> 4 #include<queue> 5 using namespace std ; 6 7 int d [ 100 ] [ 100 ] ; 8 int ha ( const int x , const int y ) { 9 if ( x == 0 && y == 0 ) return 0 ; 10 typedef pair < int , int > p ; 11 queue < p > q ; 12 for ( int i = 0 ; i < 100 ; ++ i ) 13 for ( int y = 0 ; y < 100 ; ++ y ) d [ i ] [ y ] = - 1 ; 14 d [ 50 ] [ 50 ] = 0 ; 15 q . push ( make_pair ( 50 , 50 ) ) ; 16 while ( q . size () ) { 17 const p o = q . front () ; q . pop () ; 18 if ( abs ( o . first - 50 ) > 40 || abs ( o . second - 50 ) > 40 ) continue ; 19 const int x_ [] = { 1 , 1 , 2 , 2 , -2 , -2 , -1 , -1 } ; 20 const int y_ [] = { 2 , -2 , 1 , -1 , 1 , -1 , 2 , -2 } ; 21 for ( int i = 0 ; i < 8 ; ++ i ) { 22 const int nx = o . first + x_ [ i ] ; 23 const int ny = o . second + y_ [ i ] ; 24 if ( nx == x + 50 && ny == y + 50 ) return d [ o . first ] [ o . second ] + 1 ; 25 if ( d [ nx ] [ ny ] == - 1 ) { 26 d [ nx ] [ ny ] = d [ o . first ] [ o . second ] + 1 ; 27 q . push ( make_pair ( nx , ny ) ) ; 28 } 29 } 30 } 31 exit ( -1 ) ; 32 } 33 34 int xp , yp , xs , ys ; 35 int x , y ; 36 int step_cnt = 0 ; 37 int main () { 38 scanf ( "%d%d%d%d" , & xp , & yp , & xs , & ys ) ; 39 x = abs ( xp - xs ) ; y = abs ( yp - ys ) ; 40 while ( abs ( x ) + abs ( y ) >= 30 ) { 41 if ( abs ( x ) >= abs ( y ) ) { 42 const int step = abs ( x / 6 ) ; 43 x -= ( ( x > 0 ) ? step : - step ) * 2 ; 44 y -= ( y > 0 ) ? step : - step ; 45 step_cnt += abs ( step ) ; 46 } else { 47 const int step = abs ( y / 6 ) ; 48 x -= ( x > 0 ) ? step : - step ; 49 y -= ( y > 0 ? step : - step ) * 2 ; 50 step_cnt += abs ( step ) ; 51 } 52 } 53 printf ( "%d\n" , step_cnt + ha ( abs ( x ) , abs ( y ) ) ) ; 54 return 0 ; 55 }
贪心+bfs