UVA 11796
題意:? 有兩個狗, 按照 多邊形跑,不知道兩條狗的速度,但是狗是同時出發,同時到達終點的
????????? 輸出兩條狗的 最大相距距離 - 最小相距距離;
思路 : 用物理的相對運動來計算, 每次只計算 兩條狗的直線運動, 轉折點再額外更新
LRJ 模板大法好 !!!LRJ 模板大法好 !!!!LRJ 模板大法好 !!!!
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 100; const double eps = 1e-9;struct Point {double x , y;Point (double x = 0, double y = 0) : x(x),y(y) {} };typedef Point Vector;int dcmp (double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x,A.y+B.y); } Vector operator - (Point A, Point B) { return Vector(A.x-B.x,A.y-B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p,A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p,A.y/p); } bool operator < (const Point &a, const Point &b) {return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator == (const Point &a, const Point &b) {return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }double Dot (Vector A,Vector B) { return A.x*B.x + A.y*B.y; } double Length (Vector A) { return sqrt(Dot(A,A)); } double Angle (Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); } double Cross (Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double DistanceToSegment (Point P, Point A, Point B) { ///點到線段的距離if(A == B) return Length(P - A);Vector v1 = B - A, v2 = P - A, v3 = P - B;if(dcmp(Dot(v1,v2)) < 0 ) return Length(v2);else if(dcmp(Dot(v1,v3)) > 0) return Length(v3);else return fabs(Cross(v1,v2)) / Length(v1); }double MAX,MIN;void UpDate(Point P, Point A, Point B) {MIN = min(MIN, DistanceToSegment(P,A,B)); ///當前 和 點 到線段的最小距離MAX = max(Length(P-A),MAX);MAX = max(Length(P-B),MAX); }Point Pa[maxn], Pb[maxn];int main() {int t;scanf("%d",&t);for(int kase = 1; kase <= t; kase++){int A, B;scanf("%d %d",&A,&B);for(int i = 0; i < A; ++i) cin >> Pa[i].x >> Pa[i].y;for(int i = 0; i < B; ++i) cin >> Pb[i].x >> Pb[i].y;double LenA = 0, LenB = 0;for(int i = 0; i < A-1; ++i) LenA += Length(Pa[i] - Pa[i+1]);for(int i = 0; i < B-1; ++i) LenB += Length(Pb[i] - Pb[i+1]);MAX = -1e9; MIN = 1e9;int sa = 0, sb = 0; ///下一個轉折點Point Na = Pa[0], Nb = Pb[0]; /// 起始位置while(sa < A-1 && sb < B-1){double La = Length(Pa[sa+1] - Na); /// a 當前到下一個轉折點的長度double Lb = Length(Pb[sb+1] - Nb); /// b ...double t = min(La / LenA, Lb / LenB); /// 運動的時間Vector Va = (Pa[sa+1] - Na) / La * t * LenA;/// a 的位移量Vector Vb = (Pb[sb+1] - Nb) / Lb * t * LenB;/// b 的位移量UpDate(Na,Nb,Nb+Vb-Va); /// B相對A 的運動就是 NB + Vb - Va;Na = Na + Va; /// 更新 a 的當前點Nb = Nb + Vb;if(Na == Pa[sa+1]) sa++; ///如果到了轉折點, sa 下一個轉折點更新if(Nb == Pb[sb+1]) sb++;}printf("Case %d: %.0lf\n",kase, MAX - MIN);}return 0; }轉載于:https://www.cnblogs.com/aoxuets/p/5506893.html
總結
- 上一篇: Fetch API
- 下一篇: Android毛玻璃处理代码(Blur)