UVa 10180 - Rope Crisis in Ropeland!
生活随笔
收集整理的這篇文章主要介紹了
UVa 10180 - Rope Crisis in Ropeland!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&page=show_problem&problem=1121
題意:給出兩點坐標,用一條最短的線(曲線或者直線)連接起來,坐標系中原點處有一個半徑為R的圓,連線不能穿過這個圓。
分析:
這題我WA了好幾次,原因是把double的坐標搞成int了,o(╯□╰)o
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 5 const double PI = acos( -1.0 ); 6 7 struct point 8 { 9 double x, y; 10 }; 11 12 point O; //坐標原點 13 14 double Get_Dist( point &a, point &b ) //計算兩點間距離 15 { 16 return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y )*(a.y - b.y) ); 17 } 18 19 double Get_Angle( double a, double b, double c ) //余弦定理求角度 20 { 21 return acos( (b*b + c*c - a*a) / (2*b*c) ); 22 } 23 24 bool check( point &m, point &n, double x, double y ) //判斷坐標( x, y )是否在點 m, n 之間 25 { 26 double minx = m.x < n.x ? m.x : n.x; 27 double maxx = m.x > n.x ? m.x : n.x; 28 double miny = m.y < n.y ? m.y : n.y; 29 double maxy = m.y > n.y ? m.y : n.y; 30 return ( x > minx && x <= maxx && y >= miny && y <= maxy ); 31 } 32 33 bool Judge( point &m, point &n, double &R ) //判斷是否需要繞過圓 34 { 35 if ( m.x == n.x ) //特判與y軸平行的線 36 { 37 if ( abs(m.x) >= R ) return true; 38 else 39 { 40 if ( m.y * n.y > 0 ) return true; 41 else return false; 42 } 43 } 44 else if ( m.y == n.y ) //特判與x軸平行的線 45 { 46 if ( abs(m.y) >= R ) return true; 47 else 48 { 49 if ( m.x * n.x > 0 ) return true; 50 else return false; 51 } 52 } 53 54 double A = n.y - m.y; //兩點連成的直線,求直線方程一般式,計算A, B, C 55 double B = m.x - n.x; 56 double C = (n.x - m.x) * m.y - (n.y - m.y) * m.x; 57 double dis = abs(C)/ sqrt( A*A + B*B ); //原點到直線的距離 58 if ( dis >= R ) return true; 59 else 60 { 61 double kkk = (-A) / B; 62 double bbb = (-C) / B; 63 double xx = ( -bbb ) / ( kkk + 1.0 / kkk ); 64 double yy = ( (-1.0) / kkk ) * xx; 65 if ( check( m, n, xx, yy ) ) return false; //如果線段穿過圓 66 else return true; //如果線段不穿過圓 67 } 68 } 69 70 int main() 71 { 72 int T; 73 double R; 74 point A, B; 75 O.x = 0; 76 O.y = 0; 77 scanf( "%d", &T ); 78 while ( T-- ) 79 { 80 scanf( "%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &R ); 81 82 if ( Judge(A, B, R) ) //如果不需要繞過圓 83 printf( "%.3f\n", Get_Dist(A, B) ); 84 else //如果需要繞過圓 85 { 86 double L1 = Get_Dist(A, O); 87 double L2 = Get_Dist(B, O); 88 double LL1 = sqrt( L1*L1 - R*R ); 89 double LL2 = sqrt( L2*L2 - R*R ); 90 double angle = Get_Angle( Get_Dist(A, B), L1, L2 ) - acos( R/L1 ) - acos( R/L2 ); 91 double ans = LL1 + LL2 + R * angle; 92 printf( "%.3f\n", ans ); 93 } 94 } 95 return 0; 96 }
轉載于:https://www.cnblogs.com/GBRgbr/archive/2012/08/21/2649587.html
總結
以上是生活随笔為你收集整理的UVa 10180 - Rope Crisis in Ropeland!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 亏赚 进来看看吧 80分
- 下一篇: 一个鸡蛋多少钱啊?