Computational Geometry--bnu51638

Source: Internet
Author: User
Tags arithmetic data structures min printf set time time limit wrapper

Air Hockey Time limit:1000ms Memory limit:262144kb 64-bit integer IO format: %lld      Java class N ame:  Main Special Judge prev  submit  status  statistics  discuss   Next type:  None N Onegraph theory     2-sat    articulation/bridge/biconnected Component     cycles/topological sorting/strongly Connected Component     shortest Path         bellman ford         dijkstra/floyd Warshall     euler trail/circuit     heavy-light Decomposition    minimum Spanning Tree     stable marriage Problem      trees    directed Minimum Spanning Tree     flow/ Matching         graph matching            bipartite Matching              hopcroft–karp bipartite Matching              weighted bipartite Matching/hungarian Algorithm          flow             max Flow/Min Cut             min cost Max Flow dfs-like      Backtracking with Pruning/branch and Bound     basic recursion    ida* Search     parsing/grammar    breadth First Search/depth First Search      advanced Search Techniques         binary Search/Bisection         ternary Search Geometry     basic Geometry    computational Geometry     convex Hull      pick ' s theoremgame theory     green hackenbush/colon principle/fusion Principle      nim    sprague-grundy number Matrix    gaussian Elimination     matrix Exponentiationdata Structures     basic Data Structures    binary Indexed tree     binary Search Tree      hashing    orthogonal Range Search     range Minimum Query/Lowest Common Ancestor     segment tree/interval tree    trie Tree      sorting     disjoint SetString     aho Corasick     knuth-morris-pratt     suffix array/suffix Treemath     BasicMath    big Integer arithmetic     number theory         chinese remainder theorem         extended Euclid       nbsp; Inclusion/Exclusion         modular arithmetic     combinatorics          Group theory/burnside ' s lemma        counting      probability/expected Value Others    tricky     Hardest     unusual     brute force    implementation      Constructive Algorithms    two Pointer     Bitmask     beginner     discrete Logarithm/shank ' s baby-step giant-step Algorithm   &NBSp; greedy    divide and conquer Dynamic programming  Tag It!

Bored river boat classmates and bored fish swell students are very fond of playing table hockey (actually just like listening to the sound of the ball collision). In the boring day, the bored river boat students think of a boring play: two people at the same time two balls on the table, while hitting out, and then listen to the two balls hit together when the sound. However, they are not very good at the accuracy of the shot, so the two balls will not be able to collide.

Now assume that the desktop is infinitely large and absolutely smooth, giving the initial position, radius, and movement speed of the two balls, ensuring that the two balls are initially untouched. Bored river boat students want to know whether two goals can collide (contact is think of collision), if can, he wants to know two ball collision time (from two people to start timing), if not, he would like to know the whole process of the minimum distance between two balls, here the two ball distance is defined as two balls take two points distance of the minimum value, The data guarantees that in this case the answer is not less than. Note that the puck is a cylinder, which looks down from the air as a circle, and in this problem, the height of the puck is negligible. Input

The first line is a positive integer that represents the number of groups of test data,

Each set of test data contains two rows,

The first line contains five integers that are not greater than the absolute value, representing the initial position, radius, and motion velocity of the first ball. Output

For each set of test data, if two balls can collide, output two ball collision time, otherwise the output of the whole process of the two ball distance of the minimum, relative error does not exceed, that is, the output is, the standard answer is, if satisfied, the output will be considered the correct answer. Sample Input

2
0 0 2 1 0 each
0 1-1 0
0 0 2 1 0
11 5 1-1 0
Sample Output
4.0000000000
2.0000000000
Hint

For the first set of samples, the two balls collide 4 seconds after the hit,

For the second set of samples, the two balls do not collide and the two balls are closest to each other 5.5 seconds after hitting the ball, and the distance is 2.0. Source 14th Beijing Normal University Program design competition final Author quailty Solution:

Set time is T two ball collision or two ball distance nearest, with T can represent two ball coordinates Q1 (x1+t*vx1, Y1+t*vy1) Q2 (x2+t*vx2, Y2+t*vy2),

Q1Q2 distance = aqrt{(x1-x2) ^2 + (y1-y2) ^2} =r1+r2; The collation equation gets a unary two-time equation about T sqrt (a t^2 + b T + c) = R1+R2;

Solve the equation, divide a=0 and a. =0,a.   =0, solving <0, =0, >0, when <0 without solution, solve the equation to the left of the minimum value, that is, T =-b/(2*a), t>0, bring into the equation d= sqrt ((4ac-b^2)/(4a)), ans=d-(R1+R2); T<0, Ans=sqrt (c)-(R1+R2). When >=0, Ans=min (T1,T2). When A=0, Ans=sqrt (c)-(R1+R2).

Also reviewed the middle school knowledge

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> using namespace std
;
    Double Solve (double a,double b,double c) {double dat=b*b-4.0*a*c;
    if (dat<0) return-1;
    Double x1= (-b+sqrt (DAT))/2.0/a;
     Double x2= (-b-sqrt (DAT))/2.0/a;
    if (x2>=0.0) return x2;
    if (x2<0&&x1>0) return x1;
if (x1<0&&x2<0) return-1;
    } int main () {int t;
    Double x1,y1,r1,vx1,vy1;
    Double x2,y2,r2,vx2,vy2;
    Double X,vx,y,vy;
    Double a,b,c;
    Double R;
    scanf ("%d", &t);
        while (t--) {scanf ("%lf%lf%lf%lf%lf", &x1,&y1,&r1,&vx1,&vy1);
        scanf ("%lf%lf%lf%lf%lf", &x2,&y2,&r2,&vx2,&vy2);
        x=x1-x2;///c y=y1-y2;///c vx=vx1-vx2;///a vy=vy1-vy2;///a a=vx*vx+vy*vy;
        C=x*x+y*y;
        b=2.0* (X*vx+y*vy);
        r=1.0* (R1+R2) * (R1+R2); Double t=-1.0*b/2.0/A;
            if (a!=0) {double ans=solve (a,b,c-r);

            if (ans<0&&t<0) printf ("%.10lf\n", sqrt (c)-r1-r2);

            if (ans<0&&t>=0) printf ("%.10lf\n", sqrt ((4.0*a*c-b*b)/(4.0*a))-R1-R2);
        if (ans>=0) printf ("%.10lf\n", ans);
    } else printf ("%.10lf\n", sqrt (c)-r1-r2);
} return 0;
 }


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.