HDU 4454 Stealing a Cake(枚举角度)

题目链接

去年杭州现场赛的神题。。枚举角度。。精度也不用注意。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define PI 3.1415926
double xl,xr,yl,yr;
double judge(double tx,double ty)
{
    if(tx < xl&&ty < yl)
    return sqrt((tx-xl)*(tx-xl)+(ty-yl)*(ty-yl));
    else if(tx >= xl&&tx <= xr&&ty >= yr)
    return ty - yr;
    else if(tx >= xl&&tx <= xr&&ty <= yl)
    return yl - ty;
    else if(tx > xr&&ty > yr)
    return sqrt((tx-xr)*(tx-xr)+(ty-yr)*(ty-yr));
    else if(ty >= yl&&ty <= yr&&tx >= xr)
    return tx - xr;
    else if(ty >= yl&&ty <= yr&&tx <= xl)
    return xl - tx;
    else if(tx > xr&&ty < yl)
    return sqrt((tx-xr)*(tx-xr)+(ty-yl)*(ty-yl));
    else if(tx < xl&&ty > yr)
    return sqrt((tx-xl)*(tx-xl)+(ty-yr)*(ty-yr));
    return 10000000;
}
int main()
{
    double sx,sy,rx,ry,r;
    int i;
    double tx,ty,minz,temp;
    while(scanf("%lf%lf",&sx,&sy)!=EOF)
    {
        if(sx == 0&&sy == 0) break;
        minz = 1000000000;
        scanf("%lf%lf%lf",&rx,&ry,&r);
        scanf("%lf%lf%lf%lf",&xl,&yl,&xr,&yr);
        if(xl > xr) swap(xl,xr);
        if(yl > yr) swap(yl,yr);
        for(i = 0;i < 36000;i ++)
        {
            tx = rx+r*cos(i*1.0*PI/18000);
            ty = ry+r*sin(i*1.0*PI/18000);
            temp = sqrt((tx-sx)*(tx-sx) + (ty-sy)*(ty-sy));
            minz = min(minz,temp+judge(tx,ty));
        }
        printf("%.2f\n",minz);
    }
    return 0;
}

 

 

posted @ 2013-10-09 17:29  Naix_x  阅读(179)  评论(0编辑  收藏  举报