2C. Commentator problem【模拟退火(枚举)】

博客介绍了如何使用模拟退火算法解决一个几何问题:寻找一个点,该点到三个圆的视角相等。在奥运会期间,评论员需要找到最佳观察点,使他们能同时观察到三个圆形体育场,且视角相同。文章提到了数学解法的复杂性,并推荐使用模拟退火算法作为暴力枚举的优化方法,尽管这种方法存在失败概率。

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

C. Commentator problem
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.

Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.

Input

The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x,  y,  r, where (x, y) are the coordinates of the stadium's center ( -  103 ≤ x,  y ≤ 103), and r(1 ≤ r  ≤ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.

Output

Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.

Examples
input
0 0 10
60 0 10
30 30 10
output
30.00000 0.00000

题意:

给出三个圆的坐标以及半径,让你找到一个点,使得这个点到三个圆的视角相等(视角:从一点引圆的切线,两条切线的夹角),求这个点的坐标


题解:

如果用数学方法解几何问题,是可以做出来的,但是会很麻烦,看到别人写的模拟退火算法,学习了


感觉就是一种暴力枚举的方法,只是在枚举中,每次都向最优解进行搜索,但是这个算法是概率算法,有一定的失败概率,成功与否取决于枚举的策略

算法讲解点这里


/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-5;
struct point
{
    double x,y;
    double r;
}p[3];
double dist(double x,double y,point tp)
{
    return sqrt((x-tp.x)*(x-tp.x)+(y-tp.y)*(y-tp.y));
}
double cost(double x,double y)
{
    double ang[3];//当前解对应的三个视角
    for(int i=0;i<3;++i)
    {
        ang[i]=dist(x,y,p[i])/p[i].r;
    }
    double d[3];//视角的差值
    for(int i=0;i<3;++i)
    {
        d[i]=ang[i]-ang[(i+1)%3];
    }
    double ans=0;
    for(int i=0;i<3;++i)
    {
        ans+=d[i]*d[i];//找到一个定量来表示误差
    }
    return ans;
}
int main()
{
    while(~scanf("%lf%lf%lf",&p[0].x,&p[0].y,&p[0].r))
    {
        for(int i=1;i<3;++i)
        {
            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].r);
        }
        double x=0,y=0;
        for(int i=0;i<3;++i)
        {
            x+=p[i].x/3;y+=p[i].y/3;//先大约确定最优解可能的范围,大约是重心附近
        }
        double t=1.0;
        while(t>eps)
        {
            int kase=0;
            double tp=cost(x,y);//当前的误差
            if(cost(x+t,y)<tp)//四个方向进行尝试枚举
            {
                x+=t;kase=1;
            }
            else if(cost(x-t,y)<tp)
            {
                x-=t;kase=1;
            }
            else if(cost(x,y+t)<tp)
            {
                y+=t;kase=1;
            }
            else if(cost(x,y-t)<tp)
            {
                y-=t;kase=1;
            }
            if(!kase)//如果没有更优解,缩小尝试的范围
            {
                t/=2;
            }
        }
        if(fabs(cost(x,y)<eps))//查看是否足够精确
        {
            printf("%lf %lf\n",x,y);
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值