[poj 1379]Run Away[模拟退火]

题意:

找出矩形中与所有点距离最远的点.

思路:

仍然是模拟退火, 需要更改的就是估价函数, 精度D&边界问题(求最小值的话不需要额外限制边界,因为边界外的肯定没有边界内的距离短嘛,但是求最长距离的话就有可能选择边界外的点了), 判断条件.

/*
...that has the maximal distance from all the holes中,和所有点的距离定义为所有点距中最小的
*/

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
const int NUM=20;
const int RAD=1000;
const double INF = 1e18;
struct point
{
    double x,y,val;
    point() {}
    point(double _x,double _y):x(_x),y(_y) {}
} p[1005],May[NUM],e1,e2;
int n,m;
double x,y;
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double judge(point t)//评价函数,得到点t的评价值val
{
    double len=INF;
    for(int i=0; i<m; i++)
        len=min(len, dis(t,p[i]));
    return len;
}
double Rand()
{
    return rand()%(RAD+1)/(1.0*RAD);   //随机产生0-1的浮点数
}
point Rand_point(point a,point b)//在a,b框定的四边形内随机生成点
{
    point tmp=point(a.x+(b.x-a.x)*Rand(),a.y+(b.y-a.y)*Rand());
    tmp.val=judge(tmp);
    return tmp;
}
void solve(double D)
{
    for(int i=0; i<NUM; i++)
    {
        May[i]=Rand_point(e1,e2);
        //printf("random point in (0, 0) and (%d, %d):\t(%.3lf, %.3lf)\n",x,y,May[i].x,May[i].y);
    }

    while(D>0.1)
    {
        for(int i=0; i<NUM; i++)
            for(int j=0; j<NUM; j++)
            {
                point tmp=Rand_point(point(max(May[i].x-D,0.0),max(May[i].y-D,0.0)),point(min(May[i].x+D,x),min(May[i].y+D,y)));
                if(tmp.val>May[i].val)
                {
                    May[i]=tmp;
                }//这里,并没有所谓的"在一定概率下接受坏的选择",否则又慢又不准.是否是这样的优化比"概率妥协"更好?
            }
        D*=0.9;
    }
    double ans=0;
    int mark,i;
    for(i=0; i<NUM; i++)
        if(May[i].val>ans)
        {
            ans=May[i].val;
            mark = i;
        }
    printf("The safest point is (%.1lf, %.1lf).\n",May[mark].x,May[mark].y);
}
int main()
{
    srand(time(0));

    scanf("%d",&n);

        while(n--)
        {
            scanf("%lf %lf %d",&x,&y,&m);
            e1 = point(0,0);
            e2 = point(x,y);
            for(int i=0; i<m; i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
            }
            solve(max(x, y));
        }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值