我比较快速的想到了三分,但是我是从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. 在linux下如何编译C++程序

    一.GCC(GNU Compiler Collection)是linux下最主要的编译工具,GCC不仅功能非常强大,结构也异常灵活.它可以通过不同的前端模块来支持各种语言,如Java.Fortran. ...

  2. javascript定义类和类的实现

    首先说说类,在一个类里我们会有以下的几个特征: 1. 公有方法 2. 私有方法 3. 属性 4. 私有变量 5. 析构函数 我们直接看一个例子: /***定义类***/ var Class = fun ...

  3. Possible concurrency problem: Replicated version id X matches in-memory version for session ...

    The message basically is saying that a replicated session is overriding an existing session in that ...

  4. for循环遍历字符串的还有一种方法

    遍历字符c,让它各自等于字符串数组chars里面的各个字符.然后运行以下的语句,当c被赋值为chars里面全部字符各一次后.就会退出这个循环. 通常我们遍历字符串数组用 for(int i=0;i&l ...

  5. uva 705

    题意,给你迷宫算出其中个封闭空间的个数,以及求出所有封闭的空间的最大步长,但是给你的迷宫式“/”,“\”来标记的所以需要将每个格子分开来3*3的格子来算, 一开始按照2*2来算,2*2有临界情况不好算 ...

  6. CF437D(The Child and Zoo)最小生成树

    题目: D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. [置顶] Codeforces 70D 动态凸包 (极角排序 or 水平序)

    题目链接:http://codeforces.com/problemset/problem/70/D 本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包 判断:平衡树log(n)内选出点 ...

  8. Android L动画入门

    Android L带来了许多新特性,其中就包括了大量的动画效果,你可以在自己的应用中使用.本文中我将详解这些动画和如何在应用中使用.本文中的所有代码可以在github上找到. 波纹和强调 现在安卓支持 ...

  9. HashSet的排序

    import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util ...

  10. ABAP函数:VIEW_MAINTENANCE_CALL(维护表视图等)

    function:VIEW_MAINTENANCE_CALL 功能:维护表视图等 The function module calls the extended table maintenance (V ...