您的位置:首页 > 其它

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

2013-08-28 21:34 316 查看
题意:

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

思路:

仍然是模拟退火, 需要更改的就是估价函数, 精度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));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: