HDU 4454 Stealing a Cake(枚举角度)

时间:2021-08-30 12:13:47

题目链接

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

#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 ;
}
int main()
{
double sx,sy,rx,ry,r;
int i;
double tx,ty,minz,temp;
while(scanf("%lf%lf",&sx,&sy)!=EOF)
{
if(sx == &&sy == ) break;
minz = ;
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 = ;i < ;i ++)
{
tx = rx+r*cos(i*1.0*PI/);
ty = ry+r*sin(i*1.0*PI/);
temp = sqrt((tx-sx)*(tx-sx) + (ty-sy)*(ty-sy));
minz = min(minz,temp+judge(tx,ty));
}
printf("%.2f\n",minz);
}
return ;
}