UVA - 11796 - Dog Distance （计算几何~）

~~~~~~~~~~计算几何 同时被 2 个专栏收录
11 篇文章 0 订阅
89 篇文章 0 订阅

AC代码：

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x) , y(y) { }
};

typedef Point Vector;

Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Vector A, Vector 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);
}

const double eps = 1e-10;
int dcmp(double x) {
if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

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 Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); }

Vector Rotate(Vector A, double rad) {
}

Vector Normal(Vector A) {
double L = Length(A);
return Vector(-A.y/L, A.x/L);
}

Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}

double DistanceToLine(Point P, Point A, Point B) {
Vector v1 = B-A, v2 = P - A;
return fabs(Cross(v1,v2) / Length(v1));
}

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);
}

Point GetLineProjection(Point P, Point A, Point B) {
Vector v = B - A;
return A + v * ( Dot(v, P-A) / Dot(v, v) );
}

bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}

bool OnSegment(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

double ConvexPolygonArea(Point* p, int n) {
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area / 2;
}

const int maxn = 60;
int T, A, B;
Point P[maxn], Q[maxn];
double Min, Max;

void update(Point P, Point A, Point B) {
Min = min(Min, DistanceToSegment(P, A, B));
Max = max(Max, Length(P-A));
Max = max(Max, Length(P-B));
}

int main() {
scanf("%d", &T);
for(int cas = 1; cas <= T; cas++) {
scanf("%d %d", &A, &B);
for(int i = 0; i < A; i++) scanf("%lf %lf", &P[i].x, &P[i].y);
for(int i = 0; i < B; i++) scanf("%lf %lf", &Q[i].x, &Q[i].y);

double lenA = 0, lenB = 0;
for(int i = 0; i < A-1; i++) lenA += Length(P[i+1]-P[i]);
for(int i = 0; i < B-1; i++) lenB += Length(Q[i+1]-Q[i]);

int Sa = 0, Sb = 0;//Sa，Sb为刚经过的拐点的编号
Point Pa = P[0], Pb = Q[0];//Pa，Pb为甲乙现在的位置
Min = 1e9, Max = -1e9;

while(Sa < A-1 && Sb < B-1) {
double La = Length(P[Sa+1] - Pa);//甲到下一个拐点的距离
double Lb = Length(Q[Sb+1] - Pb);//乙到下一个拐点的距离
double T = min(La/lenA, Lb/lenB);//可以先让甲和乙的速度分别是lenA和lenB

Vector Va = (P[Sa + 1] - Pa) / La * T * lenA;//甲的位移向量，(P[Sa+1]-Pa)/La为单位向量
Vector Vb = (Q[Sb + 1] - Pb) / Lb * T * lenB;//乙的位移向量，(Q[Sb+1]-Pb)/Lb为单位向量

update(Pa, Pb, Pb + Vb - Va);				 //更新最大最小距离

Pa = Pa + Va;
Pb = Pb + Vb;
if(Pa == P[Sa+1]) Sa++;
if(Pb == Q[Sb+1]) Sb++;
}
printf("Case %d: %.0lf\n", cas, Max - Min);//%.0lf可以表达四舍五入
}
return 0;
} 

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-23 850

08-17 574
05-24 23
10-02 433
10-24 1129
08-19 1388
09-08 494
04-18 922
02-19 412
02-11 1170
09-01 820
04-21 22
08-15 919
03-20 2100