码迷,mamicode.com
首页 > 其他好文 > 详细

Hdu 4454 Stealing a Cake(枚举或三分)

时间:2016-08-10 11:06:38      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:枚举角度,确定圆上点的位置,取最小值。

点到矩形最短距离:若点在矩形边所表示的范围内,则到矩形最短距离为x或y坐标到矩形边的距离。否则为点到矩形顶点的距离。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const double pi=acos(-1.0);
struct Point
{
    double x,y,r;
};
Point C,P,st;
double x1,y1,x2,y2;
double miny,maxy,minx,maxx;
double dist(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Rdist(Point a)
{
    double x=0.0,y=0.0;
    if(a.x<minx) x=minx-a.x;
    else if(a.x>maxx) x=a.x-maxx;
    if(a.y<miny) y=miny-a.y;
    else if(a.y>maxy) y=a.y-maxy;
    return sqrt(x*x+y*y);
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    while(scanf("%lf%lf",&st.x,&st.y)!=EOF)
    {
        if(st.x==0&&st.y==0) break;
        double ans=1e10;
        scanf("%lf%lf%lf",&C.x,&C.y,&C.r);
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        minx=min(x1,x2),maxx=max(x1,x2);
        miny=min(y1,y2),maxy=max(y1,y2);
        for(double rad=0; rad<=360; rad+=0.005)
        {
            Point P;
            P.x=C.x+C.r*cos(rad/180*pi);
            P.y=C.y+C.r*sin(rad/180*pi);
            ans=min(ans,dist(P,st)+Rdist(P));
        }
        printf("%.2f\n",ans);
    }
    return 0;
}



Hdu 4454 Stealing a Cake(枚举或三分)

标签:

原文地址:http://blog.csdn.net/wang2147483647/article/details/52168996

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!