Uva 11796 Dog Distance(几何+相对运动)

本文针对UVA在线评测中的一道经典题目进行了解析,该题涉及两狗追逐过程中的最大与最小相对距离计算。通过将路径划分为多个线段并模拟两狗在各线段上的相对运动,利用几何方法确定不同状态下的最大和最小距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2896

思路:若两只狗只在一条线段上运动,由于求两狗相对距离,假设以狗A为系,则狗B相对A运动的速度为vB-vA,狗A相对静止。则此时相当于一静止点到一线段的距离,最小值出现在点到线段的距离,最大值出现在点到两端点的距离。所以按拐点将两狗的路线分为若干段,则每段都为两狗分别在两线段上运动。所以,按照两狗到达拐点的位置模拟,比较其到达先后顺序,并更新当前位置即可。(两狗同时到达,则速度可分别表示为狗A的路线长度lenA、狗B的路线长度lenB)

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;

struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {}
    void read()
    {
        scanf("%lf%lf",&x,&y);
    }
    void print()
    {
      printf("%.6f %.6f",x,y);
    }
};

typedef Point Vector;

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

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

Vector Rotate(Vector A,double rad)
{
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

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

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

const int maxn=50+50;

int A,B;
double lenA,lenB;
double nextlenA,nextlenB;
Point pA[maxn],pB[maxn];
double minans,maxans;
double TimeA,TimeB,Time;
Vector xA,xB;


void solve(Point p,Point a,Point b)
{
  minans=min(minans,DistanceToSegment(p,a,b));
  maxans=max(maxans,Length(p-a));
  maxans=max(maxans,Length(p-b));
}

void init()
{
  minans=INF;
  maxans=-INF;
  lenA=lenB=0.0;
}

int main()
{
#ifdef debu
    freopen("in.txt","r",stdin);
#endif // debug
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
      init();
      scanf("%d%d",&A,&B);
      for(int i=1;i<=A;i++) pA[i].read();
      for(int i=1;i<=B;i++) pB[i].read();
      for(int i=1;i<A;i++)
      {
        lenA+=Length(pA[i+1]-pA[i]);
      }
      for(int i=1;i<B;i++)
      {
        lenB+=Length(pB[i+1]-pB[i]);
      }
      int pointA=1,pointB=1;
      Point posA=pA[1],posB=pB[1];
      while(pointA<A&&pointB<B)
      {

        nextlenA=Length(pA[pointA+1]-posA);
        TimeA=nextlenA/lenA;
        nextlenB=Length(pB[pointB+1]-posB);
        TimeB=nextlenB/lenB;
        Time=min(TimeA,TimeB);

        xA=(pA[pointA+1]-posA)/nextlenA*Time*lenA;
        xB=(pB[pointB+1]-posB)/nextlenB*Time*lenB;

        solve(posA,posB,posB+xB-xA);

        posA=posA+xA;
        posB=posB+xB;

        if(posA==pA[pointA+1]) pointA++;
        if(posB==pB[pointB+1]) pointB++;

      }
      printf("Case %d: %.0f\n",++cas,maxans-minans);

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值