我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次

后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值

以下是代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
using namespace std; typedef struct
{
double x;
double y;
}point; typedef struct
{
double x;
double y;
double r;
}circle; typedef struct
{
point s;
point e;
}line; double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double cal(point st,line ed,circle c,double t)
{
point p;
p.x=c.x+cos(t)*c.r;
p.y=c.y+sin(t)*c.r;
if(fabs(ed.s.x-ed.e.x)<1e-8)
{
if(p.y>ed.s.y&&p.y<ed.e.y)
{
return dis(p,st)+fabs(p.x-ed.s.x);
}
else
{
double t=min(dis(p,ed.s),dis(p,ed.e));
return dis(p,st)+t;
} }
if(fabs(ed.s.y-ed.e.y)<1e-8)
{
if(p.x>ed.s.x&&p.x<ed.e.x)
{
return dis(p,st)+fabs(p.y-ed.s.y);
}
else
{
double t=min(dis(p,ed.s),dis(p,ed.e));
return dis(p,st)+t;
}
}
return -1;
} double solve1(point st,line ed,circle c)
{
double L=0,R=pi,mid=(L+R)/2,midmid=(mid+R)/2;
while(R-L>1e-10)
{
if(cal(st,ed,c,mid)<cal(st,ed,c,midmid))R=midmid;
else L=mid;
mid=(L+R)/2;
midmid=(R+mid)/2;
}
return cal(st,ed,c,midmid);
} double solve2(point st,line ed,circle c)
{
double L=pi,R=2*pi,mid=(L+R)/2,midmid=(mid+R)/2;
while(R-L>1e-10)
{
if(cal(st,ed,c,mid)<cal(st,ed,c,midmid))R=midmid;
else L=mid;
mid=(L+R)/2;
midmid=(R+mid)/2;
}
return cal(st,ed,c,midmid);
} int main()
{
double m,x[2],y[2];
point st;
circle cir;
line a[4];
while(scanf("%lf%lf",&st.x,&st.y)==2)
{
if(!st.x&&!st.y)break;
scanf("%lf%lf%lf",&cir.x,&cir.y,&cir.r);
scanf("%lf%lf",&x[0],&y[0]);
scanf("%lf%lf",&x[1],&y[1]);
if(x[0]>x[1]) swap(x[0],x[1]);
if(y[0]>y[1]) swap(y[0],y[1]);
a[0].s.x=x[0];a[0].s.y=y[0];
a[0].e.x=x[0];a[0].e.y=y[1];
a[1].s.x=x[0];a[1].s.y=y[0];
a[1].e.x=x[1];a[1].e.y=y[0];
a[2].s.x=x[1];a[2].s.y=y[0];
a[2].e.x=x[1];a[2].e.y=y[1];
a[3].s.x=x[0];a[3].s.y=y[1];
a[3].e.x=x[1];a[3].e.y=y[1];
m=1e18;
for(int i=0;i<4;i++)
{
double t=solve1(st,a[i],cir);
if(t<m)m=t;
t=solve2(st,a[i],cir);
if(t<m)m=t;
}
printf("%.2lf\n",m);
}
return 0;
}

HDU 4454 - Stealing a Cake(三分)的更多相关文章

  1. hdu 4454 Stealing a Cake (三分)

    Stealing a Cake Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 4454 Stealing a Cake(三分之二)

    pid=4454" target="_blank" style="">题目链接:hdu 4454 Stealing a Cake 题目大意:给定 ...

  3. hdu 4454 Stealing a Cake(计算几何:最短距离、枚举/三分)

    题意:已知起点.圆.矩形,要求计算从起点开始,经过圆(和圆上任一点接触即可),到达矩形的路径的最短距离.(可以穿过园). 分析:没什么好的方法,凭感觉圆上的每个点对应最短距离,应该是一个凸函数,用三分 ...

  4. hdu 4454 Stealing a Cake 三分法

    很容易想到三分法求解,不过要分别在0-pi,pi-2pi进行三分. 另外也可以直接暴力枚举…… 代码如下: #include<iostream> #include<stdio.h&g ...

  5. hdu 4454 Stealing a Cake

    简单的计算几何: 可以把0-2*pi分成几千份,然后找出最小的: 也可以用三分: #include<cstdio> #include<cmath> #include<al ...

  6. hdu 4454 Stealing a Cake(三分法)

    给定一个起始点,一个矩形,一个圆,三者互不相交.求从起始点->圆->矩形的最短距离. 自己画一画就知道距离和会是凹函数,不过不是一个凹函数.按与水平向量夹角为圆心角求圆上某点坐标,[0, ...

  7. HDU 4454 Stealing a Cake(枚举角度)

    题目链接 去年杭州现场赛的神题..枚举角度..精度也不用注意.. #include <iostream> #include <cstdio> #include <cstr ...

  8. HDU 4454 Stealing a Cake --枚举

    题意: 给一个点,一个圆,一个矩形, 求一条折线,从点出发,到圆,再到矩形的最短距离. 解法: 因为答案要求输出两位小数即可,精确度要求不是很高,于是可以试着爆一发,暴力角度得到圆上的点,然后求个距离 ...

  9. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

随机推荐

  1. JavaScript 进阶(五)易混淆概念null vs undefined, == vs ===, string vs String

    先看一段代码 var foo = {} foo.a = null alert(foo.a == null)     //true alert(foo.a === undefined)  //false ...

  2. Ubuntu 14.04LTS Gnome GUI初体验及163更新源配制

    Ubuntu 14.04 LTS于前天(2014.4.17)公布, 我今天将我的系统升级到最新, 体验了下最新的UI系统. 我选择了Ubuntu Gnome 的GUI界面.我曾经的系统是12.04lt ...

  3. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. frame.bounds和center

    CGPoint point=CGPoint(x,y);  //表示位置 CGSize size=CGSzieMake(width,height);  //表示大小 CGRect rect=CGRect ...

  5. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  6. mysql5.6 主从配置

    参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...

  7. Java实现定时任务的三种方法(转)

    在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...

  8. fake it till you become it

    fake it till you become it_你泛起山川烟波里的不是我._百度空间 fake it till you become it

  9. 3xian退役贴【深思。】

    这是原文: 最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong Answer的纠缠.还好lynn ...

  10. Redis intset

    Redis intset也非常简单.它是一个有序整数集合,实际上是一个有序数组,查找时二分查找,添加时可能需要内存挪动.唯一不同的是,它的整数类型是变动的,可以支持int16,int32,int64三 ...