POJ-1379 Run Away 计算几何 模拟退火

POJ-1379 Run Away

题意: 平面上给定n个点, 要求找到一个点使得到所有点的距离最大。
分析: 模拟退火, 计算到每个点的最小距离, 使得最小距离最大。
代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

const int MAXN = 1111;
const double eps = 1e-9;
const double inf = 0x3f3f3f3f;
struct Point
{
    double x, y;
    double distance (Point b)
    {
        return hypot (x - b.x, y - b.y);
    }
};

Point pp[MAXN];

int dx[] = {0, 0, 1, 1, -1, -1, 1, -1};
int dy[] = {1, -1, -1, 1, 1, -1, 0, 0};
double mindis (int n, Point now)
{
    double res = inf;
    for (int i = 0; i < n; i++)
    {
        res = min (res, now.distance(pp[i]));
    }
    return res;
}
int main ()
{
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        double X, Y;
        int n;
        scanf ("%lf%lf%d", &X, &Y, &n);

        for (int i = 0; i < n; i++)
            scanf ("%lf%lf", &pp[i].x, &pp[i].y);
        double step = 100000, r = 0.9;
        double x = 0, y = 0;
        Point now;
        now.x = x, now.y = y;
        double ans = mindis(n, now);

        while (step > eps)
        {
            double checkx = x, checky = y, checkans = ans;

            for (int i = 0; i < 8; i++)
            {
            
            now.x = x + step*dx[i];
            now.y = y + step*dy[i];
                if (now.x >=0 && now.x < X + eps && now.y >= 0 && now.y < Y + eps)
                {
                    double tmp = mindis (n, now);
                    if (tmp > checkans)
                    {
                        checkx = now.x;
                        checky = now.y;
                        checkans = tmp;
                    }
                }
            }
            x = checkx;
            y = checky;
            ans = checkans;
            step*=r;
        }
        printf ("The safest point is (%.1f, %.1f).\n", x, y);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值