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): 3227    Accepted Submission(s): 895



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
2012 Asia Hangzhou Regional Contest

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4454

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

解题思路:
  很明显的三分题,三分与二分都比较类似,三分求极值。

困惑点:
三分有两种写法,一种是平均三分,在区间里分成三等分。
另一种是取中点,再取中点与右端点的中点进行三分。
在这题里,第一种可以过,第二种不能过。

代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;
const int maxn=100019;
int num[maxn],ans[maxn],C[maxn],B[maxn];
char s[maxn];
typedef long long ll;
const double eps=1e-8;
double x,y,circlex,circley,circler,rectx1,rectx2,recty1,recty2;
const double PI=acos(-1.0);
double cal(double angle)
{
    double xx=circlex+circler*cos(angle);
    double yy=circley+circler*sin(angle);
    double res,ans;
    res=sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
    if(xx<rectx1)
    {
        if(yy<recty1)
            ans=sqrt((xx-rectx1)*(xx-rectx1)+(yy-recty1)*(yy-recty1));
        else if(yy>=recty1&&yy<=recty2)
           ans=rectx1-xx;
        else
            ans=sqrt((xx-rectx1)*(xx-rectx1)+(yy-recty2)*(yy-recty2));

    }
    else if(xx>=rectx1&&xx<=rectx2)
    {
        if(yy<recty1)
            ans=recty1-yy;
        else
            ans=yy-recty2;
    }
    else if(xx>rectx2)
    {
        if(yy<recty1)
            ans=sqrt((xx-rectx2)*(xx-rectx2)+(yy-recty1)*(yy-recty1));
        else if(yy>=recty1&&yy<=recty2)
           ans=xx-rectx2;
        else
            ans=sqrt((xx-rectx2)*(xx-rectx2)+(yy-recty2)*(yy-recty2));
    }
    return ans+res;
}
int main()
{
    while(scanf("%lf%lf",&x,&y)!=EOF)
    {
        if(x==0&&y==0)
            return 0;
        scanf("%lf%lf%lf",&circlex,&circley,&circler);
        scanf("%lf%lf%lf%lf",&rectx1,&recty1,&rectx2,&recty2);
        if(rectx1>rectx2)
            swap(rectx1,rectx2);
        if(recty1>recty2)
            swap(recty1,recty2);
        double l=0,r=2*PI,mid,mimid,mid1,mid2;
        while(r-l>eps)
        {
            double z=mid=(r-l)/3.0;
            mid=l+z;mimid=r-z;
            if(cal(mid)<cal(mimid))
                r=mimid;
            else
                l=mid;
        }
        mid1=mid;

        double res=cal(mid1);
        printf("%.2f\n",res);
        /*
        double l=0,r=2*PI,mid,mmid;
        double res=cla(l);
        while(r-l>1e-8)
        {
            mid=(r+l)/2;
            mmid=(r+mid)/2;
            double k1=cla(mid),k2=cla(mmid);
            if(k1>k2)
                l=mid;
            else
                r=mmid;
        }
        printf("%.2lf\n",cla(mid));*/
    }
    return 0;
}





1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值