#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<ctime>
#pragma warning(disable:4996)
#define EPS 1e-8
#define INF 2000000000
#define PI acos(-1.0)
const int N = 30;
const int M = 30;
using namespace std;
double X, Y;
class Point
{
public:
double x, y;
bool isIn()
{
if (x > -EPS&&x<EPS + X&&y>-EPS&&y < EPS + Y)
{
return true;
}
return false;
}
};
double dist(const Point &p1,const Point &p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
double getMinDist(const Point &p,const vector<Point>&point)
{
double ans = INF;
for (size_t i = 0; i < point.size(); i++)
{
ans = min(ans, dist(point[i], p));
}
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin);
int T; cin >> T;
srand((unsigned)time(NULL));
while (T--)
{
int n;
cin >> X >> Y >> n;
vector<Point>trap(n);//各陷阱的坐标
for (int i = 0; i < n; i++)
{
cin >> trap[i].x >> trap[i].y;
}
//随机取各随机位置的坐标,并求得到该位置到离它最近的陷阱的距离
vector<Point>random(N);
vector<double>minidist(N);
for (int i = 0; i < N; i++)
{
random[i].x = (rand() % 10000) / 10000.0*X;
random[i].y = (rand() % 10000) / 10000.0*Y;
minidist[i] = getMinDist(random[i],trap);
}
double delta = max(X, Y) / (double)n;
while (delta>1e-3)
{
for (int i = 0; i<N; i++)
{
for (int j = 0; j<M; j++)
{
double theta = (rand() % 10000 ) / 10000.0 * 2 * PI;
Point cur;
cur.x = random[i].x + cos(theta)*delta;
cur.y = random[i].y + sin(theta)*delta;
if (!cur.isIn()) continue;
double tmp = getMinDist(cur,trap);
if (tmp>minidist[i])
{
random[i] = cur;
minidist[i] = tmp;
}
}
}
delta *= 0.85;
}
int index = 0;
double ans = 0;
for (int i = 0; i<N; i++)
{
if (minidist[i]>ans)
{
ans = minidist[i];
index = i;
}
}
printf("The safest point is (%.1f, %.1f).\n", random[index].x, random[index].y);
}
return 0;
}
poj_1379_run away
最新推荐文章于 2018-04-12 22:11:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)