| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求高人指点,怎么老是错呢?我的想法是假如最安全的点是A,那么所有点到他的最短距离最长,如是,我们作圆,
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int X=0,Y=0,m=0;
int p[1001][2]={{0}};
double point[3][2] = {{0}};
double compute(),binary(double i,double j);
int check(int i,double R2),contact(int i,int j,double R);
int main()
{
int i = 0,j = 0,testcases = 0;
scanf("%d",&testcases);
for (i=1;i<=testcases;i++)
{
scanf("%d%d%d",&X,&Y,&m);
for (j=1;j<=m;j++)
{
scanf("%d%d",&p[j][0],&p[j][1]);
if (p[j][0]>X||p[j][1]>Y)
{
j--,m--;
}
}
compute();
printf("The safest point is (%0.1lf, %0.1lf).\n",point[2][0],point[2][1]);
}
return 0;
}
double compute()
{
if (m != 1)
{
int i,j,pppppoint[4][2] = {{0,0},{0,Y},{X,Y},{X,0}};
double R2 = 0,minR2=X*X+Y*Y,realR2;
for (j=0;j<=3;j++)
{
minR2=X*X+Y*Y;
for (i=1;i<=m;i++)
{
realR2 = (pppppoint[j][0]-p[i][0])*(pppppoint[j][0]-p[i][0])
+ (pppppoint[j][1]-p[i][1])*(pppppoint[j][1]-p[i][1]);
if (minR2>realR2)minR2 = realR2;
}
if (minR2>R2)
{
R2 = minR2;
point[2][0]=pppppoint[j][0],point[2][1]=pppppoint[j][1];
}
}
return binary(sqrt(R2),sqrt(1.0*X*X+Y*Y));
}
else
{
if (fabs(p[1][0])>fabs(p[1][0]-X))point[2][0] = 0;
else point[2][0] = X;
if ( fabs(p[1][1])>fabs(p[1][1]-Y))point[2][1] = 0;
else point[2][1] = Y;
return 0;
}
}
int contact(int i,int j,double R)
{
double a,b,l2,x1,y1,r2,c;
a = 1.0 * (p[i][0] - p[j][0]);
b = 1.0 * (p[i][1] - p[j][1]);
l2= a*a + b*b;
x1 = 0.5 * (p[i][0] + p[j][0]);
y1 = 0.5 * (p[i][1] + p[j][1]);
r2 = R*R - l2 / 4.0;
if (r2 < -0.001 )return 0;
(r2>0)?(c = sqrt ((r2) / l2)):( c = 0);
point[0][0] = x1 + b*c;
point[0][1] = y1 - a*c;
point[1][0] = x1 - b*c;
point[1][1] = y1 + a*c;
return 1;
}
double binary(double i,double j)
{
double k = 0.5*(i+j);
int a=0,b=0;
if (j-i<=0.04)return k;
for (a=1;a<m;a++)
for (b=a+1;b<=m;b++)
{
if (((p[a][0]-p[b][0])||(p[a][1]-p[b][1]))&&contact(a,b,k))
{
if (check(1,k*k)||check(0,k*k))
return binary(k,j);
}
}
return binary(i,k);
}
int check(int i,double R2)
{
int j;
if ( point[i][0]>X||point[i][0]<0
|| point[i][1]>Y||point[i][1]<0)
return 0;
double k = 0;
for (j=1;j<=m;j++)
{
k = (pow(point[i][0]-p[j][0],2)+pow(point[i][1]-p[j][1],2));
if (k < R2-0.001 )
return 0;
}
point[2][0] = point[i][0],point[2][1] = point[i][1];
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator