HDU 1109 Run Away

本文介绍了一种使用随机搜索方法解决寻找矩阵区域内最安全点的问题,通过不断随机生成新点并评估其安全性来逐步逼近最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给一个矩阵的长宽,再给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).\n",ans.x,ans.y);
    }
    return 0;
}

转载于:https://www.cnblogs.com/cjjsb/p/9513667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值