首页 > ACM题库 > HDU-杭电 > HDU 4454-Stealing a Cake-搜索-[解题报告]HOJ
2015
07-16

HDU 4454-Stealing a Cake-搜索-[解题报告]HOJ

Stealing a Cake

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

1.75
2.00

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

 

题意:给一个点,一个圆和一个矩形,矩形与圆没有重叠部分,求从该点出发经过圆上一点再到矩形边上一点的距离和的最小值。

 

分析:在区间[0,2*PI]内三分角度即可。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <iomanip>
#include <math.h>

using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);

struct Point
{
    double x,y;
};

struct Line
{
    Point a,b;
};

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 cross(Point A,Point B,Point C)
{
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}

double distToLine(Point p,Line s)
{
    Point t = p;
    t.x += s.a.y - s.b.y;
    t.y += s.b.x - s.a.x;
    if(cross(s.a,t,p)*cross(s.b,t,p) > eps)
        return dist(p,s.a) < dist(p,s.b) ? dist(p,s.a) : dist(p,s.b);
    return fabs(cross(p,s.a,s.b))/dist(s.a,s.b);
}

Point O,cir,A,B;
Line s[4];
Point p[4];
double r;

void Import()
{
    cin>>cir.x>>cir.y>>r;
    cin>>A.x>>A.y>>B.x>>B.y;
    if(A.y < B.y) swap(A,B);
    p[0].x = A.x;
    p[0].y = B.y;
    p[1].x = B.x;
    p[1].y = B.y;
    p[2].x = B.x;
    p[2].y = A.y;
    p[3].x = A.x;
    p[3].y = A.y;
    s[0].a = p[0];
    s[0].b = p[1];
    s[1].a = p[1];
    s[1].b = p[2];
    s[2].a = p[2];
    s[2].b = p[3];
    s[3].a = p[3];
    s[3].b = p[0];
}

double equ(double alpha)
{
    Point tmp;
    tmp.x = cir.x + r*cos(alpha);
    tmp.y = cir.y + r*sin(alpha);
    double d1 = dist(O,tmp);
    double ans = 99999999;
    for(int i=0;i<4;i++)
       ans = min(ans,distToLine(tmp,s[i]));
    return d1+ans;
}

double ternarySearch(double l,double r)
{
    while(r-l>eps)
    {
        double ll=(2*l+r)/3;
        double rr=(l+2*r)/3;
        double ans1=equ(ll);
        double ans2=equ(rr);
        if(ans1 > ans2)
            l=ll;
        else
            r=rr;
    }
    return l;
}

void Work()
{
    Import();
    cout<<fixed<<setprecision(2)<<equ(ternarySearch(0,2*PI))<<endl;
}

int main()
{
    while(true)
    {
        cin>>O.x>>O.y;
        if(fabs(O.x)<eps && fabs(O.y)<eps) break;
        Work();
    }
    return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/acdreamers/article/details/12278015