hdu 4454 Stealing a Cake (三分)

Stealing a Cake

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1164    Accepted Submission(s): 320


Problem Description
There 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.
 
Input
The 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.
 
Output
For 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 1 0 -1 1 0 0 2 -1 1 1 0 -1 1 0 0 0
 
Sample Output
1.75 2.00
 
Source


思路:
分析之后,是满足凸型的函数,用三分求最小值。(听说暴力枚举角度也可以过,那就没意思了)

ps:
比赛时,所有的代码都写好了,但是一直WA,一直卡到了比赛结束,好桑心。比赛完后发现是自己矩阵的坐标没统一,我想的是统一,敲却用错了变量,真的太马虎了,把峰峰和泉泉给坑了,真的太不好意思了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 10005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.00000001
using namespace std;

int n,m;
double x,y,xr,yr,r,x1,Y1,x2,y2,ans;
double le,ri,mid,mmid;

double caldist(double xx1,double YY1,double xx2,double yy2)
{
    return sqrt((xx1-xx2)*(xx1-xx2)+(YY1-yy2)*(YY1-yy2));
}
double getdist(double k)
{
    int i,j;
    double xx,yy,d1,d2;
    xx=xr+r*cos(k);
    yy=yr+r*sin(k);
    d1=caldist(xx,yy,x,y);
    d2=INF;
    if(xx>=x1&&xx<=x2||yy>=Y1&&yy<=y2)
    {
        if(xx>=x1&&xx<=x2)
        {
            d2=min(d2,fabs(yy-Y1));
            d2=min(d2,fabs(yy-y2));
        }
        else
        {
            d2=min(d2,fabs(xx-x1));
            d2=min(d2,fabs(xx-x2));
        }
    }
    else
    {
        d2=min(d2,caldist(xx,yy,x1,Y1));
        d2=min(d2,caldist(xx,yy,x2,y2));
        d2=min(d2,caldist(xx,yy,x1,y2));
        d2=min(d2,caldist(xx,yy,x2,Y1));
    }
    return d1+d2;
}
void solve()
{
    int i,j;
    double d1,d2,tle=le,tri=ri;
    while(ri-le>eps)
    {
        mid=(le+ri)/2.0;
        mmid=(mid+ri)/2.0;
        d1=getdist(mid);
        d2=getdist(mmid);
        if(d1<d2) ri=mmid;
        else le=mid;
    }
    ans=d1;
    le=tri;
    ri=tle+2*pi;
    while(ri-le>eps)
    {
        mid=(le+ri)/2.0;
        mmid=(mid+ri)/2.0;
        d1=getdist(mid);
        d2=getdist(mmid);
        if(d1<d2) ri=mmid;
        else le=mid;
    }
    ans=min(ans,d1);
}
void presolve()
{
    int i,j;
    double xx1=x1,YY1=Y1,xx2=x2,yy2=y2,tmp;
    x1=min(xx1,xx2);
    Y1=min(YY1,yy2);
    x2=max(xx1,xx2);
    y2=max(YY1,yy2);
    xx1=xr-x;
    YY1=yr-y;
    tmp=2*xx1/(2*sqrt(xx1*xx1+YY1*YY1));
    le=pi/2+acos(tmp);
    ri=le+pi;
}
int main()
{
    int i,j;
    while(scanf("%lf%lf",&x,&y))
    {
        if(x==0&&y==0) break ;
        scanf("%lf%lf%lf%lf%lf%lf%lf",&xr,&yr,&r,&x1,&Y1,&x2,&y2);
        presolve();
        solve();
        printf("%.2f
",ans);
    }
    return 0;
}







 
原文地址:https://www.cnblogs.com/riasky/p/3361047.html