PKU 1379 Run Away(模拟退火算法)
2024-08-27 22:21:01
题目大意:原题链接
给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大.
解题思路:一开始想到用随机化算法解决,但是不知道如何实现。最后看了题解才知道原来是要用模拟退火算法解决。
不过个人感觉这个算法的实现过程中仍然采用了随机化算法。二者均属于概率算法。 参考链接
Point Goto_Rand_Dir(double key,Point temp)函数中,Point temp必须得定义在参数中,不能定义在函数内部,
否则temp没有初始值,无法进行后面的加法运算.
#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstdlib>
const int shift=;
const double inf=1e10;
const double pi=acos(-1.0);
struct Point
{
double x,y;
}p[],randp[]; double Get_Dis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
Point Get_Rand_Point(int a,int b)
{
Point temp;
temp.x=rand()%a+;
temp.y=rand()%b+;
return temp;
}
Point Goto_Rand_Dir(double key,Point temp)
{
double delta=*pi*(double)rand()/RAND_MAX;
temp.x+=key*sin(delta);
temp.y+=key*cos(delta);
return temp;
} int main()
{
double dis[];
int i,j,k,T,x,y,m;
scanf("%d",&T);
srand(time(NULL));
while(T--){
double now;
Point temp;
scanf("%d%d%d",&x,&y,&m);
for(i=;i<=m;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=;i<=;i++){//分别往四个方向找四个方向的最小值
dis[i]=inf;
randp[i]=Get_Rand_Point(x,y);//给randp[]数组随机取横纵坐标值
for(j=;j<=m;j++){
now=Get_Dis(randp[i],p[j]);
if(now<dis[i]) dis[i]=now;//dis[i]存的是最小值
}
}
double key=sqrt(1.0*(x*x+y*y))/;//关键
while(key>=0.01){//精度要求
for(i=;i<=;i++){
for(j=;j<=shift;j++){
temp=randp[i];//采用之前四个方向确定的随机横纵坐标值
temp=Goto_Rand_Dir(key,temp);
if(temp.x<||temp.y<||temp.x>x||temp.y>y)
continue;
now=inf;
for(k=;k<=m;k++){
double dist=Get_Dis(temp,p[k]);
if(now>dist) now=dist;//now存的是最小值
}
if(now>dis[i]){
dis[i]=now;//dis[i]中存的是最小值最大
randp[i]=temp;
}
}
}
key=key*0.8;
}
for(i=,k=;i<=;i++)//在四个方向中找最小值最大
if(dis[i]>dis[k]) k=i;
printf("The safest point is (%.1lf, %.1lf).\n",randp[k].x,randp[k].y);
}
}
最新文章
- svn比对
- Windows Server 2003 服务器备份和恢复技巧
- Maximum Subsequence Sum(接上篇)
- zoj.3868.GCD Expectation(数学推导>;>;容斥原理)
- JAVA-- M选N的组合算法
- scala特质
- Jquery实现的Tabs标签页
- ionicPopup确认对话框
- To fix sql server 2008 r2 Evaluation period has expired by change the key
- 使用SQLServer 2008的CDC功能实现数据变更捕获
- Vim的基本使用(一)
- DLC 复合逻辑运算
- 查询当前局域网下所有IP和物理网卡地址
- leetCode- 472. Concatenated Words
- Sql server bulk insert
- 元组(Tuple)
- informix中的时间计算
- Windows 平台下安装Cygwin后,sshd服务无法启动
- Objective-C:协议protocol
- Iterator 迭代器模式 MD
热门文章
- 学习:erlang读取文件中的terms
- .NET Entity Framework(EF)使用SqlQuery直接操作SQL查询语句或者执行过程
- 目标检测YOLOv1-v3——学习笔记
- 从头认识java-18.2 主要的线程机制(2)-Executors的使用
- ionic ngRepeat追加数据(加载更多,不需要重复渲染dom数据)
- Deep Learning的基本思想
- 常用MySQL函数
- js i++ 与 ++i 的区别
- 160524、Linux下如何启动、关闭Oracle以及打开关闭监听
- 160509、Java过滤器与SpringMVC拦截器之间的关系与区别