"Training Guide"--8.4

Source: Internet
Author: User

Uva 11796:

A and b two dogs were running along a line. Two dogs have a speed position, but they are known to start at the same time and arrive at a constant pace. Your task is to find out the difference between A and B's maximum distance and the nearest distance during the run.

Analysis: Here to analyze the distance problem of two dogs in the exercise process, we are faced with the following two difficulties:

(1) Two dogs are moving.

(2) The dog's movement line is a polyline.

In order to solve the first problem, we consider the motion relativity in physics, we assume that two dogs are currently walking straight (simplified model), we will keep the dog as stationary, do its direction of motion vector of the opposite vector, and the other dog's motion vector to synthesize, Then the synthesized vector path can accurately represent the relative position of two dogs, then the problem of solving the maximum minimum value of the distance between the more difficult two moving points is transformed into the maximum or minimum value of the vertex to determine the distance of the segment.

For the second problem, we only need to divide the entire motion process one by one into the "simplified model" given above in the process of simulating the calculation.

So now we're going to have a more detailed discussion at the code level:

How to solve the shortest distance (the longest distance) of a vertex to a line segment?

The longest distance is very good understanding, must be this vertex to the line segment of the two endpoints of one of the lines, this does not have to explain, here we mainly analyze the shortest distance.

First we have to list all the things in the geometry so that we can implement the code.

The following is a simple algebra operation based on graphs.

A few points to note is that the situation here actually contains p on a, B. From the textbook of calculus, the cross-multiplication of vectors is a vector, but here we get the number by the cross-multiplication, so the upper fork takes the outer "| |" The symbol is the absolute meaning.

The simple reference code is as follows (with small bugs):

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>using namespacestd;Const DoubleEPS = 1e-8;Const intMAXN = -;intT, A, B;intDCMP (Doublex) {if(Fabs (x) <eps)return 0;return(x<0)?-1:1;}structpoint{Doublex, y; Point (Double_x=0,Double_y=0): X (_x), Y (_y) {};}; Point P[MAXN], Q[MAXN];DoubleMin, Max; Pointoperator+ (point A,point B) {returnPoint (a.x+b.x,a.y+b.y);} Pointoperator-(point A,point B) {returnPoint (a.x-b.x,a.y-b.y);} Pointoperator* (Point A,DoubleP) {returnPoint (a.x*p,a.y*p);} Pointoperator/(Point A,DoubleP) {returnPoint (a.x/p,a.y/p);}BOOL operator< (ConstPoint&a,ConstPOINT&AMP;B) {returna.x<b.x| | (a.x==b.x&&a.y<b.y);}BOOL operator==(ConstPoint&a,ConstPOINT&AMP;B) {returnDCMP (a.x-b.x) = =0&AMP;&AMP;DCMP (a.y-b.y) = =0;}DoubleCross (vector A, vector B) {returna.x*b.y-a.y*b.x;}DoubleDot (vector A, vector B) {returna.x*b.x + a.y*b.y;}DoubleLength (Vector) {returnsqrt (Dot (A, a));}Doubledistancetosegment (Point P, point A, point B) {if(A = = B)returnLength (P-A); Vector v1= b-a, v2 = P-a,v3 = PB; if(DCMP (Dot (V1,V2)) <0)returnLength (v2); Else if(DCMP (Dot (v1,v3)) >0)returnLength (v3); Else returnFabs (Cross (v1, v2))/Length (v1);}voidUpdate (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));}intMain () {scanf ("%d",&T);  for(intKase =1; Kase <= t;kase++) {scanf ("%d%d",&a,&B);  for(inti =0; i < a;i++) scanf ("%d%d",&p[i].x,&p[i].y);  for(inti =0; i < b;i++) scanf ("%d%d",&q[i].x,&q[i].y); DoubleLenA =0, LenB =0;  for(inti =0; I < A-1; i++) LenA + = Length (p[i+1] -P[i]);  for(inti =0; I < B-1; i++) LenB + = Length (q[i+1] -Q[i]); intSa =0, Sb =0; Point Pa= p[0], Pb = q[0]; Min= 1e9, Max =-1e9;  while(Sa < A-1&& Sb < B-1)             {                  DoubleLa = Length (p[sa+1]-Pa); DoubleLb = Length (q[sb+1]-Pb); DoubleT = min (La/lena, lb/LenB); Vector Va= (p[sa+1]-PA)/la*t*LenA; Vector Vb= (q[sa+1]-QA)/lb*t*LenB; 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", Kase, Max-Min); }    return 0;}

"Training Guide"--8.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.