HDU 4454Stealing a Cake(枚举或者三分)

本文探讨了一个蚂蚁从起点出发,通过圆蛋糕到达目的地并返回蚂蚁之家的最短路径问题。通过数学计算,求解了蚂蚁最优路径长度。

Stealing a Cake

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


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
 
题目大意:给你一个蚂蚁的x,y坐标,再给你一个圆蛋糕的圆心坐标与半径,再给你一个
矩形的两个对角线的坐标,矩形表示蚂蚁的家。问你这只蚂蚁跑到蛋糕那里再回到家
的最短路线是多少。抽象出来就是在圆上找一个点,使得这一点到蚂蚁初始坐标的距
离与这一点到蚂蚁家的距离之和最短。


解题思路:题目中说了圆和矩形不相交,点不在圆内不在矩形内。所以不需要讨论那么
多的情况。圆上一点到家的距离,一点到矩形的距离,可以抽象成到四条线段的距离,
分为八种情况讨论,这点分别在矩形的左上,正左边,左下,右上,正右边,右下,
正上方,正下方。具体实现见代码,自己开始是枚举圆上的点,每次角度变化
为PI/1000。开始角度变化为PI/100WA了一发。在不超时的情况下,尽量分成更多的
块。 还有一种是三分的情况,不过单调性实际上是见个凹函数,因为从蚂蚁这个方向
向圆做切线,两个切点靠蚂蚁的方向有一个最小值,但是在靠蚂蚁的反方向还有一
个最小值。但是我们要求的实际上是前者!所以在三分的时候需要把圆分成两半再分
别三分,取两者中较小的。具体实现见代码。


题目地址:Stealing a Cake



枚举AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
double xx1,yy1,xx2,yy2;
const double eps=1e-6;
const double PI=acos(-1.0);
struct Point
{
    double x;
    double y;
};
Point ant;

struct mm
{
    double x;
    double y;
    double r;   //圆的半径
};
mm cake;

double cal(double angle)
{
    double sum1,sum2,ans;
    double xx,yy;   //表示圆上角度刚好为angle的点
    xx=cake.x+cake.r*cos(angle);
    yy=cake.y+cake.r*sin(angle);

    sum1=sqrt((xx-ant.x)*(xx-ant.x)+(yy-ant.y)*(yy-ant.y));  //sum1为ant到cake的距离

    sum2=0;    //sum2为cake到home的距离
    if(xx<xx1)  //在矩形的左半边
    {
        if(yy>=yy1&&yy<=yy2)
            sum2=xx1-xx;
        else if(yy<yy1)
            sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy1)*(yy-yy1));
        else
            sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy2)*(yy-yy2));
    }
    else if(xx>xx2)   //在矩形的右半边
    {
        if(yy>=yy1&&yy<=yy2)
            sum2=xx-xx2;
        else if(yy<yy1)
            sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy1)*(yy-yy1));
        else
            sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy2)*(yy-yy2));
    }
    else
    {
        if(yy<=yy1)
            sum2=yy1-yy;
        else
            sum2=yy-yy2;
    }
    ans=sum1+sum2;
    return ans;
}

int main()
{
    double txx1,tyy1,txx2,tyy2;
    while(~scanf("%lf%lf",&ant.x,&ant.y))
    {
        if(ant.x==0&&ant.y==0) break;
        scanf("%lf%lf%lf",&cake.x,&cake.y,&cake.r);
        scanf("%lf%lf%lf%lf",&txx1,&tyy1,&txx2,&tyy2);
        //xx1,yy1,xx2,yy2; 两个对角线构成home矩形
        xx1=txx1<txx2?txx1:txx2;
        yy1=tyy1<tyy2?tyy1:tyy2;
        xx2=txx1>txx2?txx1:txx2;
        yy2=tyy1>tyy2?tyy1:tyy2;
        double res=100000000;
        for(double t=0;t<=2*PI;t+=PI/1000.0)
        {
            double tmp=cal(t);
            if(tmp<res)
                res=tmp;
        }
        printf("%.2f\n",res);
    }
    return 0;
}

//31MS


三分AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
double xx1,yy1,xx2,yy2;
const double eps=1e-8;
const double PI=acos(-1.0);
struct Point
{
    double x;
    double y;
};
Point ant;

struct mm
{
    double x;
    double y;
    double r;   //圆的半径
};
mm cake;

double cal(double angle)
{
    double sum1,sum2,ans;
    double xx,yy;   //表示圆上角度刚好为angle的点
    xx=cake.x+cake.r*cos(angle);
    yy=cake.y+cake.r*sin(angle);

    sum1=sqrt((xx-ant.x)*(xx-ant.x)+(yy-ant.y)*(yy-ant.y));  //sum1为ant到cake的距离

    sum2=0;    //sum2为cake到home的距离
    if(xx<xx1)  //在矩形的左半边
    {
        if(yy>=yy1&&yy<=yy2)
            sum2=xx1-xx;
        else if(yy<yy1)
            sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy1)*(yy-yy1));
        else
            sum2=sqrt((xx-xx1)*(xx-xx1)+(yy-yy2)*(yy-yy2));
    }
    else if(xx>xx2)   //在矩形的右半边
    {
        if(yy>=yy1&&yy<=yy2)
            sum2=xx-xx2;
        else if(yy<yy1)
            sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy1)*(yy-yy1));
        else
            sum2=sqrt((xx-xx2)*(xx-xx2)+(yy-yy2)*(yy-yy2));
    }
    else
    {
        if(yy<=yy1)
            sum2=yy1-yy;
        else
            sum2=yy-yy2;
    }
    ans=sum1+sum2;
    return ans;
}

int main()
{
    double txx1,tyy1,txx2,tyy2;
    while(~scanf("%lf%lf",&ant.x,&ant.y))
    {
        if(ant.x==0&&ant.y==0) break;
        scanf("%lf%lf%lf",&cake.x,&cake.y,&cake.r);
        scanf("%lf%lf%lf%lf",&txx1,&tyy1,&txx2,&tyy2);
        //xx1,yy1,xx2,yy2; 两个对角线构成home矩形
        xx1=txx1<txx2?txx1:txx2;
        yy1=tyy1<tyy2?tyy1:tyy2;
        xx2=txx1>txx2?txx1:txx2;
        yy2=tyy1>tyy2?tyy1:tyy2;
        double l,r,mid,mimid;
        double mid1,mid2;
         //把圆分成两半来三分,因为从0~2*PI二分,在ant的另一边也有个最小值,但那个并不是最小的
        l=0,r=PI;
        while(r-l>eps)
        {
            mid=(l+r)/2.0,mimid=(r+mid)/2.0;
            if(cal(mid)<cal(mimid))
                r=mimid;
            else
                l=mid;
        }
        mid1=mid;

        l=PI,r=2*PI;
        while(r-l>eps)
        {
            mid=(l+r)/2.0,mimid=(r+mid)/2.0;
            if(cal(mid)<cal(mimid))
                r=mimid;
            else
                l=mid;
        }
        mid2=mid;

        double res=cal(mid1)<cal(mid2)?cal(mid1):cal(mid2);
        printf("%.2f\n",res);
    }
    return 0;
}

//0MS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值