您的位置:首页 > 其它

HDU 4454 Stealing a Cake (计算几何+三分)

2013-08-26 20:26 295 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4454

题意:给你一个点,一个圆和一个矩形,让你求从这个点到圆然后再到矩形最短的距离,圆可以穿过。

题解:

1、可以将圆分解成一个一个点,然后枚举就行了。

2、将角度分解成0-PI和PI-2PI,然后每个部分三分就行了,但个人认为这个好像不科学。

枚举AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <ctime>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

typedef long long LL;
const int N=10005;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

struct node
{
double x,y;
node(){};
node(double a,double b):x(a),y(b){}
}qi,yuan,p1,p2;
double r;

double dian_dian(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double dian_ju(node t,node a,node b)
{
double x,y;
if(t.x<a.x) x=a.x-t.x;
else if(t.x>b.x)    x=t.x-b.x;
else x=0;

if(t.y<a.y) y=a.y-t.y;
else if(t.y>b.y)    y=t.y-b.y;
else    y=0;

return sqrt(x*x+y*y);
}

int main()
{
while(scanf("%lf%lf",&qi.x,&qi.y))
{
if(fabs(qi.x-0)<1e-7&&fabs(qi.y-0)<1e-7)
break;
scanf("%lf%lf%lf",&yuan.x,&yuan.y,&r);
scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
if(p1.x>p2.x)   swap(p1.x,p2.x);//这两个不可少
if(p1.y>p2.y)   swap(p1.y,p2.y);
double Min=INF;
for(double a=0;a<2*PI;a+=0.001)
{
node p=node(yuan.x+r*cos(a),yuan.y+r*sin(a));
double tmp=dian_dian(qi,p)+dian_ju(p,p1,p2);
Min=min(Min,tmp);
}
printf("%.2f\n",Min);
}
return 0;
}


三分AC代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: