• HDU 1109 Run Away


    题目大意:给一个矩阵的长宽,再给n个点,求矩阵区域内某个点到各个点的最小距离的最大值,输出所求点的坐标

    这道题我还是写了随机化乱搞,不过由于比较懒于是就没有写模拟退火,不过也是可以AC的

    我们先初始随机一个坐标并算出它的答案,然后每一次择情况随机一个步长(这个要随着时间的推移慢慢变小),然后随机角度得到新的点坐标。

    同时我们计算出新的点的答案然后和当前的答案比对一下,如果更优就选择即可。

    注意一下:这不是模拟退火,真正的模拟退火还是有一个概率接受较差解的过程的,而且是根据老天爷的规律得出的,因此比较科学。

    还是一句老话,脸黑就多rand几次,一般来说没什么大问题就过了

    CODE

    #include<cstdio>
    #include<cctype>
    #include<cstring>
    #include<cstdlib>
    #include<ctime>
    #include<cmath>
    using namespace std;
    typedef double DB;
    const DB EPS=1e-3,dlt=0.85,pi=acos(-1.0);
    const int N=1005,EXP=10000;
    struct Point
    {
        DB x,y;
    }a[N],ans;
    int t,n; DB X,Y,mx;
    inline DB random(DB l,DB r)
    {
        return (DB)(rand()%EXP)/EXP*(r-l)+l;
    }
    inline DB min(DB a,DB b)
    {
        return a<b?a:b;
    }
    inline DB dis(Point A,Point B)
    {
        return (DB)sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
    }
    inline DB calc(Point s)
    {
        DB d=dis(s,a[1]);
        for (register int i=2;i<=n;++i)
        d=min(d,dis(s,a[i])); return d;
    }
    inline DB Simulate_Anneal(Point &s)
    {
        DB d=calc(s),step=X>Y?X:Y;
        while (step>EPS)
        {
            for (register int i=1;i<=50;++i)
            {
                DB angle=random(0,2*pi),x=s.x+cos(angle)*step,y=s.y+sin(angle)*step;
                if (x>=0&&x<=X&&y>=0&&y<=Y&&calc((Point){x,y})>d) s=(Point){x,y},d=calc(s);
            }
            step*=dlt;
        }
        return d;
    }
    int main()
    {
        register int i; srand(time(0)); scanf("%d",&t);
        while (t--)
        {
            scanf("%lf%lf%d",&X,&Y,&n); mx=0;
            for (i=1;i<=n;++i)
            scanf("%lf%lf",&a[i].x,&a[i].y);
            for (i=1;i<=50;++i)
            {
                Point P=(Point){random(0,X),random(0,Y)};
                DB d=Simulate_Anneal(P); if (d>mx) mx=d,ans=P;
            }
            printf("The safest point is (%.1lf, %.1lf).
    ",ans.x,ans.y);
        }
        return 0;
    }
    
    
  • 相关阅读:
    数组的学习(一)
    Servlet是线程安全吗?
    MySql用户管理:添加用户、授权、删除用户
    泛型(二)
    泛型(一)
    Spring MVC
    spring框架
    mybatis基础
    Hibernate 基础
    Java局部类
  • 原文地址:https://www.cnblogs.com/cjjsb/p/9513667.html
Copyright © 2020-2023  润新知