HDU 4454 Stealing a Cake(暴力计算几何)

思路:直接枚举圆上的角度,然后算点到圆上一点距离和圆上一点到矩形最短距离就可以了...


#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define INF 0x7fffffff
struct Point
{
	double x,y;
};
Point s,c,recta,rectb,t;
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 PointoRecDist(Point p,Point ra,Point rb)
{
	double dx=0,dy=0;
	if(p.x<ra.x)
        dx = ra.x-p.x;
	else if(p.x>rb.x)
		dx = p.x-rb.x;
	if(p.y<ra.y)
		dy = ra.y-p.y;
	else if (p.y>rb.y)
		dy = p.y-rb.y;

	return sqrt(dx*dx+dy*dy);
}

int main()
{
    while(scanf("%lf%lf",&s.x,&s.y)!=EOF)
	{
		if(s.x==0 && s.y==0)break;
		double r;
		scanf("%lf%lf%lf",&c.x,&c.y,&r);
        scanf("%lf%lf",&recta.x,&recta.y);
		scanf("%lf%lf",&rectb.x,&rectb.y);
		if (recta.x>rectb.x)
			swap(recta.x,rectb.x);
		if (recta.y>rectb.y)
			swap(recta.y,rectb.y);
        double ans = INF;
		for(double ang = 0.0;ang<=360;ang+=0.01)
		{
			t.x = c.x+cos(ang/180*pi)*r;
			t.y = c.y+sin(ang/180*pi)*r;
			ans = min(ans,PointoRecDist(t,recta,rectb)+dist(t,s));
		}
		printf("%.2lf\n",ans);
	}
}


Description

There is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal. 

The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant.
 

Input

The input consists of several test cases. 
The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant's home. 
All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant's home don't overlap or contact, and the ant's starting point also is not inside the cake or his home, and doesn't contact with the cake or his home.
If the ant touches any part of home, then he is at home. 
Input ends with a line of 0 0. There may be a blank line between two test cases.
 

Output

For each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point.
 

Sample Input

        
        
1 1 -1 1 1 0 -1 1 0 0 2 -1 1 1 0 -1 1 0 0 0
 

Sample Output

        
        
1.75 2.00
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值