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;
    }
    
  • 相关阅读:
    关于JEE web项目 Servlet中 “/” 的解释 ;
    通过session 怎么防止表单的重复提交!
    Struts hibernate Spring 框架原理
    apache DBUtils 使用例子demo
    二十三种设计模式通俗理解
    怎样将一个脚本添加到开机自启动中
    zabbix
    数据库
    nginx服务器
    Luogu P3398 仓鼠找sugar
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412928.html
Copyright © 2011-2022 走看看