Description
One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look completely harmless at the first sight. But when activated, they start to throw out very hot java, uh ... pardon, lava. Unfortunately, all known paths to the Center Room (where the Sarcophagus is) contain a trigger that activates the trap. The ACM were not able to avoid that. But they have carefully monitored the positions of all the holes. So it is important to find the place in the Large Room that has the maximal distance from all the holes. This place is the safest in the entire room and the archaeologist has to hide there.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing three integers X, Y, M separated by space. The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= M <= 1000. The numbers X and Yindicate the dimensions of the Large Room which has a rectangular shape. The number M stands for the number of holes. Then exactly M lines follow, each containing two integer numbers Ui and Vi (0 <= Ui <= X, 0 <= Vi <= Y) indicating the coordinates of one hole. There may be several holes at the same position.
Output
Print exactly one line for each test case. The line should contain the sentence "The safest point is (P, Q)." where P and Qare the coordinates of the point in the room that has the maximum distance from the nearest hole, rounded to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).
Sample Input
3
1000 50 1
10 10
100 100 4
10 10
10 90
90 10
90 90
3000 3000 4
1200 85
63 2500
2700 2650
2990 100
Sample Output
The safest point is (1000.0, 50.0).
The safest point is (50.0, 50.0).
The safest point is (1433.0, 1669.8).
题目分析
一个X*Y的网格图,X,Y<=10000,给你m个点,m<=1000,问你与m个点的欧几里得距离的最小值最大的点的坐标。多组数据T<=10。
所以这个东西叫做爬山算法?不明觉厉= =
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int t,X,Y,m;
struct your
{
int x,y;
}a[1410];
double minn,ans,ansx,ansy;
double dis(double x,double y,int num)
{
return (x-a[num].x)*(x-a[num].x)+(y-a[num].y)*(y-a[num].y);
}
double calc(double x,double y)
{
if(x>X||y>Y||x<0||y<0) return 0.0;
double maxx=100000000.0;
for(int i=1;i<=m;i++)
{
double tmp=dis(x,y,i);
if(tmp<maxx) maxx=tmp;
}
return maxx;
}
void check(double d,double &x,double &y)
{
double temp=calc(x+d,y+d),tmp,dx=x+d,dy=y+d;
tmp=calc(x-d,y+d); if(tmp>temp) temp=tmp,dx=x-d,dy=y+d;
tmp=calc(x-d,y-d); if(tmp>temp) temp=tmp,dx=x-d,dy=y-d;
tmp=calc(x+d,y-d); if(tmp>temp) temp=tmp,dx=x+d,dy=y-d;
if(temp>minn) minn=temp,x=dx,y=dy;
}
int main()
{
scanf("%d",&t);
while(t--)
{
ans=minn=0;
scanf("%d%d%d",&X,&Y,&m);
for(int i=1;i<=m;i++) scanf("%d%d",&a[i].x,&a[i].y);
int number=100000/m;
while(number--)
{
double tmp=(X+Y)*1.0;
double dx=rand()%X+1.0,dy=rand()%Y+1.0;
while(tmp>=1e-4)
check(tmp,dx,dy),tmp*=0.5;
if(minn>ans)
ans=minn,ansx=dx,ansy=dy;
}
printf("The safest point is (%.1lf, %.1lf).\n",ansx,ansy);
}
return 0;
}