HDOJ 4454 Stealing a Cake 計算幾何

來源:互聯網
上載者:User

標籤:


暴力枚舉角度.....

Stealing a CakeTime Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2495    Accepted Submission(s): 681


Problem DescriptionThere is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal. 

The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant. 
InputThe input consists of several test cases.
The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant‘s home.
All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant‘s home don‘t overlap or contact, and the ant‘s starting point also is not inside the cake or his home, and doesn‘t contact with the cake or his home.
If the ant touches any part of home, then he is at home.
Input ends with a line of 0 0. There may be a blank line between two test cases. 
OutputFor each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point. 
Sample Input
1 1-1 1 10 -1 1 00 2-1 1 10 -1 1 00 0
 
Sample Output
1.752.00
 
Source2012 Asia Hangzhou Regional Contest 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const double eps=1e-6;int dcmp(double x){    if(fabs(x)<eps) return 0;    return (x<0)?-1:1;}struct Point{    double x,y;    Point(double _x=0,double _y=0){x=_x; y=_y;}};Point operator+(Point A,Point B){return Point(A.x+B.x,A.y+B.y);}Point operator-(Point A,Point B){return Point(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 Cross(Point A,Point B){return A.x*B.y-A.y*B.x;}double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;}double Length(Point A){return sqrt(Dot(A,A));}double DistanceToSegment(Point P,Point A,Point B){    if(A==B) return Length(P-A);    Point 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 double pi=acos(-1.0);struct Circle{    Point c;    double r;    Circle(Point _c=0,double _r=0):c(_c),r(_r){}    Point point(double a)    {        return Point(c.x+cos(a)*r,c.y+sin(a)*r);    }};double a,b,c,d;Point P1;Point L[4];Circle C;int main(){    while(scanf("%lf%lf",&a,&b)!=EOF)    {        if(dcmp(a)==0&&dcmp(b)==0) break;        P1=Point(a,b);        scanf("%lf%lf%lf",&a,&b,&c);        C=Circle(Point(a,b),c);        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);        if(a>c) swap(a,c);        if(b>d) swap(b,d);        L[0]=Point(a,b); L[1]=Point(a,d);        L[2]=Point(c,d); L[3]=Point(c,b);        double delta=2.0*pi*0.0001;        double ans=1e30;        for(int i=0;i<10000;i++)        {double du=delta*i;            Point p = Point(C.c.x+cos(du)*C.r,C.c.y+sin(du)*C.r);            double Part1=DistanceToSegment(p,L[3],L[0]);for(int i=0;i<3;i++){double temp = DistanceToSegment(p,L[i],L[i+1]);Part1=min(Part1,temp);}double Part2=Length(p-P1);            ans=min(ans,Part1+Part2);        }        printf("%.2lf\n",ans);    }    return 0;}


著作權聲明:來自: 碼代碼的猿猿的AC之路 http://blog.csdn.net/ck_boss

HDOJ 4454 Stealing a Cake 計算幾何

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.