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

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

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

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

2012 Asia Hangzhou Regional Contest

#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);
    }
}
时间: 2024-10-14 07:46:29

HDU 4454 Stealing a Cake(三分暴力枚举)的相关文章

hdu 4454 Stealing a Cake(三分)

题目链接:hdu 4454 Stealing a Cake 题目大意:给定一个起始点s,一个圆形,一个矩形.现在从起点开始,移动到圆形再移动到矩形,求最短距离. 解题思路:在圆周上三分即可.即对角度[0,2*pi]三分,计算点和矩形的距离可以选点和矩形四条边的距离最短值. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std

Hdu 4454 Stealing a Cake(枚举或三分)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4454 思路:枚举角度,确定圆上点的位置,取最小值. 点到矩形最短距离:若点在矩形边所表示的范围内,则到矩形最短距离为x或y坐标到矩形边的距离.否则为点到矩形顶点的距离. #include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #

HDU 4454 Stealing a Cake --枚举

题意: 给一个点,一个圆,一个矩形, 求一条折线,从点出发,到圆,再到矩形的最短距离. 解法: 因为答案要求输出两位小数即可,精确度要求不是很高,于是可以试着爆一发,暴力角度得到圆上的点,然后求个距离,求点到矩形的距离就是求点到四边的距离然后求个最小值,然后总的取个最小值即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include &l

hdu 4968 Improving the GPA (水 暴力枚举)

题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10,但是这个又不要求顺序,所以只是枚举个数就行了.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

HDU 5616 Jam&#39;s balance(暴力枚举子集)

题目链接:点击打开链接 题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量. 秤砣可以放两边. 思路:因为n最大20, 暴力枚举子集. 因为可以放两边, 所以每次再跑一遍, 减去每个的重量, 将答案保存. 比赛的时候忘了限制边界,虽然过了终测数据, 却被人用大数据hack了(RE), 还是自己程序写的不够鲁棒, 思考的不完善. 细节参见代码: #include<cstdio> #include<cstring> #include<algori

hdu 4968 Improving the GPA(暴力枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 Problem Description Xueba: Using the 4-Point Scale, my GPA is 4.0. In fact, the AVERAGE SCORE of Xueba is calculated by the following formula: AVERAGE SCORE = ∑(Wi * SCOREi) / ∑(Wi) 1<=i<=N where S

hdu 4932 Miaomiao&#39;s Geometry(暴力枚举)

Miaomiao's Geometry                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description There are N point on X-axis . Miaomiao would like

HDOJ 4454 Stealing a Cake 计算几何

暴力枚举角度..... Stealing a Cake Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2495    Accepted Submission(s): 681 Problem Description There is a big round cake on the ground. A small ant plans to

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&