• hdu 4454 Stealing a Cake(三分法)


    给定一个起始点,一个矩形,一个圆,三者互不相交。求从起始点->圆->矩形的最短距离。

    自己画一画就知道距离和会是凹函数,不过不是一个凹函数。按与水平向量夹角为圆心角求圆上某点坐标,[0, PI] , [PI, 2*pi]两个区间的点会有两个凹函数。所以要做两次三分才行。

    #include<algorithm>
    #include<iostream>
    #include<fstream>
    #include<sstream>
    #include<cstring>
    #include<cstdlib>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<map>
    #include<set>
    #define FF(i, a, b) for(int i=a; i<b; i++)
    #define FD(i, a, b) for(int i=a; i>=b; i--)
    #define REP(i, n) for(int i=0; i<n; i++)
    #define CLR(a, b) memset(a, b, sizeof(a))
    #define LL long long
    #define PB push_back
    #define eps 1e-10
    #define debug puts("**debug**");
    using namespace std;
    const double PI = acos(-1);
    
    struct Point
    {
        double x, y;
        Point(double x=0, double y=0):x(x), y(y){}
    };
    typedef Point Vector;
    
    Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x, a.y+b.y); }
    Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x, a.y-b.y); }
    Vector operator * (Vector a, double p) { return Vector(a.x*p, a.y*p); }
    Vector operator / (Vector a, double p) { return Vector(a.x/p, a.y/p); }
    bool operator < (const Point& a, const Point& b)
    {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    int dcmp(double x)
    {
        if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1;
    }
    bool operator == (const Point& a, const Point& b)
    {
        return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
    }
    
    double Dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; }
    double Length(Vector a) { return sqrt(Dot(a, a)); }
    double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; }
    double DistanceToSegment(Point p, Point a, Point b)
    {
        if(a == b) return Length(p-a);
        Vector v1 = b-a, v2 = p-a, v3 = p-b;
        if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
        else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
        else return fabs(Cross(v1, v2)) / Length(v1);
    }
    struct Circle
    {
        Point c;
        double r;
        Circle(){}
        Circle(Point c, double r):c(c), r(r){}
        Point point(double a) //根据圆心角求点坐标
        {
            return Point(c.x+cos(a)*r, c.y+sin(a)*r);
        }
    }o;
    
    Point p, p1, p2, p3, p4, s;
    double a, b, c, d;
    
    
    double Calc(double x)
    {
        p = o.point(x);
        double d1 = DistanceToSegment(p, p1, p2),
        d2 = DistanceToSegment(p, p2, p3),
        d3 = DistanceToSegment(p, p3, p4),
        d4 = DistanceToSegment(p, p4, p1);
        //点p到矩形最近距离加上s到p距离
        return min(min(d1, d2), min(d3, d4)) + Length(s-p);
    }
    
    double solve()
    {
        double L, R, m, mm, mv, mmv;
        L = 0; R = PI;
        while (L + eps < R)
        {
            m = (L + R) / 2;
            mm = (m + R) / 2;
            mv = Calc(m);
            mmv = Calc(mm);
            if (mv <= mmv) R = mm; //三分法求最大值时改为mv>=mmv
            else L = m;
        }
        double ret = Calc(L);
        L = PI; R = 2*PI;
        while (L + eps < R)
        {
            m = (L + R) / 2;
            mm = (m + R) / 2;
            mv = Calc(m);
            mmv = Calc(mm);
            if (mv <= mmv) R = mm; 
            else L = m;
        }
        return min(ret, Calc(L));
    }
    
    int main()
    {
        while(scanf("%lf%lf", &s.x, &s.y))
        {
            if(s.x == 0 && s.y == 0) break;
            scanf("%lf%lf%lf", &o.c.x, &o.c.y, &o.r);
            scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
            //确定矩形四个点坐标,左上点开始 逆时针
            double maxx, maxy, minx, miny;
            maxx = max(a, c); maxy = max(b, d);
            minx = min(a, c); miny = min(b, d);
            p1 = Point(minx, maxy);
            p2 = Point(minx, miny);
            p3 = Point(maxx, miny);
            p4 = Point(maxx, maxy);
            double ans = solve();
            printf("%.2f
    ", ans);
        }
        return 0;
    }
    


  • 相关阅读:
    Andrew Ng机器学习week4(Neural Networks: Representation)编程习题
    linux系统下crontab 配置启动定时任务
    在python中配置tornado服务
    在python中Flask配置服务
    ltp的使用
    查看文件内容- 删除某个运行程序的所有进程-nohup后台执行程序
    机器学习和深度学习资料整理
    配置linux服务器和pycharm的连接
    计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:
    机器人的运动范围 剑指offer66题
  • 原文地址:https://www.cnblogs.com/riskyer/p/3295312.html
Copyright © 2020-2023  润新知