码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4454 Stealing a Cake(三分暴力枚举)

时间:2015-02-05 18:30:25      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:给你一个点,一个圆,一个矩形让你求出来,点到圆然后再到矩形的距离。圆和矩形你可以认为是可以穿过的。

解题思路:三分枚举点到圆的位置,然后求出来总长度,找到一个最小的长度。枚举点的坐标的时候可以枚举角度,也可以枚举坐标。总长度等于点到圆上的点的距离加上圆上的点到矩形四个线段的最小距离的和。

PS:输出%0.2lf所以精度要求不高,自我感觉枚举角度会更保证精度。

Stealing a Cake

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2332    Accepted Submission(s): 626


Problem 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
 

Source
 
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007

#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)


using namespace std;

const int maxn = 50100;


struct point
{
    double x, y;
};

struct Line
{
    point a, b;
};

point p[10];

double r;

double xmult(point p1, point p2, point xp)
{
    return (p1.x-xp.x)*(p2.y-xp.y) - (p2.x-xp.x)*(p1.y-xp.y);
}

double Distance(point a, point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)+eps);
}

double disptoseg(point xp, Line l)
{
    point t = xp;
    t.x += l.a.y-l.b.y;
    t.y += l.b.x-l.a.x;
    if(xmult(l.a, t, xp)*xmult(l.b , t, xp) > eps)
    {
        return Distance(xp, l.a) < Distance(xp, l.b)
        ? Distance(xp, l.a):Distance(xp, l.b);
    }
    return fabs(xmult(xp, l.a, l.b))/Distance(l.a, l.b);
}

bool cmp(point a, point b)
{
    if(a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

point aa;

double ShortDis(point x)
{
    Line l1, l2, l3, l4;
    l1.a.x = p[0].x;
    l1.a.y = p[0].y;
    l1.b.x = p[1].x;
    l1.b.y = p[1].y;

    l2.a.x = p[0].x;
    l2.a.y = p[0].y;
    l2.b.x = p[2].x;
    l2.b.y = p[2].y;

    l3.a.x = p[1].x;
    l3.a.y = p[1].y;
    l3.b.x = p[3].x;
    l3.b.y = p[3].y;

    l4.a.x = p[2].x;
    l4.a.y = p[2].y;
    l4.b.x = p[3].x;
    l4.b.y = p[3].y;

    double sx1 = min(disptoseg(x, l1), disptoseg(x, l2));
    double sx2 = min(disptoseg(x, l3), disptoseg(x, l4));
    return min(sx1, sx2);
}

double Del(double x)
{
    point xp;
    xp.x = x;
    xp.y = -1*sqrt(r*r-x*x);
    double sum = Distance(xp, aa);
    sum += ShortDis(xp);
    return sum;
}


double Del1(double x)
{
    point xp;
    xp.x = x;
    xp.y = sqrt(r*r-x*x);
    double sum = Distance(xp, aa);
    sum += ShortDis(xp);
    return sum;
}


int main()
{

    while(~scanf("%lf %lf",&aa.x, &aa.y))
    {
        if(aa.x == 0 && aa.y == 0)
            break;
        double dx, dy;
        scanf("%lf %lf %lf", &dx, &dy, &r);
        aa.x -= dx;
        aa.y -= dy;
        scanf("%lf %lf",&p[0].x, &p[0].y);
        p[0].x -= dx;
        p[0].y -= dy;

        scanf("%lf %lf",&p[1].x, &p[1].y);
        p[1].x -= dx;
        p[1].y -= dy;

        p[2].x = p[0].x;
        p[2].y = p[1].y;

        p[3].x = p[1].x;
        p[3].y = p[0].y;

        sort(p, p+4, cmp);

        double Min;
        double left = -r;
        double right = r;

        while(right-left > eps)
        {
            double mid = (right+left)/2.0;
            double rmid = (mid+right)/2.0;
            double x1 = Del(mid);
            double x2 = Del(rmid);

            if(x1 < x2) right = rmid;
            else left = mid;
        }
        Min = Del(right);
        left = -r;
        right = r;

        while(right-left > eps)
        {
            double mid = (right+left)/2.0;
            double rmid = (mid+right)/2.0;
            double x1 = Del1(mid);
            double x2 = Del1(rmid);

            if(x1 < x2) right = rmid;
            else left = mid;
        }


        Min = min(Min, Del1(right));

        printf("%.2lf\n",Min);
    }
}


HDU 4454 Stealing a Cake(三分暴力枚举)

标签:

原文地址:http://blog.csdn.net/xu12110501127/article/details/43529935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!