zoukankan      html  css  js  c++  java
  • hdu 4454, 离散化,枚举

    B. Stealing a Cake

    2000ms
    2000ms
    32768KB
    64-bit integer IO format: %I64d      Java class name: Main
    Font Size:
    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

    这是队友在比赛时YY出来的NC算法:把圆离散化为比如1000各点,枚举每个点即可:),然后我苦力代码了出来:)。
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<fstream>
    
    #define MAX_INT 0x7fffffff
    #define LL long long
    #define ULL unsigned long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) > (y) ? (y) : (x))
    
    using namespace std;
    
    double sx,sy;
    double rx,ry,r;
    double X1,Y1,X2,Y2;
    const double PI=3.141592653589794;
    const int n=10000;
    double INF = 66666.0;
    
    
    struct node{
        double x,y;
    }p[n+5];
    
    inline double dis(double x,double y,double xx1,double yy1){
        return sqrt((x-xx1)*(x-xx1) + (y-yy1)*(y-yy1));
    }
    
    double get_dis(double x, double y){
        double t=MIN(X1,X2);
        X2=MAX(X1,X2);
        X1=t;
    
        t=MIN(Y1,Y2);           Y2=MAX(Y1,Y2);
        Y1=t;
        if(x>=X1 && x<=X2){
            return MIN(fabs(y-Y2),fabs(y-Y1));
        }
        else if(y>=Y1 && y<=Y2){
            return MIN(fabs(x-X1),fabs(x-X2));
        }
        t=dis(x,y,X1,Y1);
        t=MIN(t, dis(x,y,X2,Y1));
        t=MIN(t, dis(x,y,X2,Y2));
        t=MIN(t, dis(x,y,X1,Y2));
        return t;
    }
    
    int main(){
    
        while(scanf(" %lf %lf",&sx,&sy)==2 && (sx||sy)){
            int i;
            scanf(" %lf %lf %lf",&rx,&ry,&r);
            scanf(" %lf %lf %lf %lf",&X1,&Y1,&X2,&Y2);
            for(int i=0; i<n; i++){
                p[i].x=rx + r*cos(2*PI*i/n);
                p[i].y=ry + r*sin(2*PI*i/n);
            }
            double ans=INF;
            for(i=0; i<n; i++){
                double tx=p[i].x,ty=p[i].y;
                ans=MIN(ans,dis(sx,sy,tx,ty)+get_dis(tx,ty));
            }
            printf("%.2f
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Devrama Slider
    全栈开发必备的10款 Sublime Text 插件
    经典网页设计:使用颜色滤镜效果的20个网站
    Nibbler – 免费的网站测试和指标评分工具
    使用 HTML5 Canvas 绘制出惊艳的水滴效果
    Qt4 和 Qt5 模块的分类
    设计Qt风格的C++API
    Qt属性系统
    Qt实现艺术字效果
    Qt中容器类应该如何存储对象(最好使用对象指针类型,如:QList<TestObj*>,而不要使用 QList<TestObj> 这样的定义,建议采用 智能指针QSharedPointer)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412928.html
Copyright © 2011-2022 走看看