山登り
class SilverDistance { public: int minSteps(int sx, int sy, int gx, int gy) { int cnt = 0; while( sx != gx || sy != gy ){ double dist[5]; int nx[5], ny[5]; const int dx[] = {-1, -1, 0, +1, +1}; const int dy[] = {-1, +1, 1, -1, +1}; for(int d=0; d<5; ++d){ nx[d] = sx + dx[d]; ny[d] = sy + dy[d]; double x = nx[d] - gx; double y = ny[d] - gy; dist[d] = sqrt( x * x + y * y ); } int mn = min_element( dist, dist + 5 ) - dist; sx = nx[mn]; sy = ny[mn]; ++cnt; } return cnt; } };
0 件のコメント :
コメントを投稿