[hdu3007]Buried memory(最小圆覆盖)--计算几何

题目描述

Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather not happened.
The world king Sconbin is not the exception.One day,Sconbin was sleeping,then swakened by one nightmare.It turned out that his love letters to Dufein were made public in his dream.These foolish letters might ruin his throne.Sconbin decided to destroy the letters by the military exercises’s opportunity.The missile is the best weapon.Considered the execution of the missile,Sconbin chose to use one missile with the minimum destruction.
Sconbin had writen N letters to Dufein, she buried these letters on different places.Sconbin got the places by difficult,he wants to know where is the best place launch the missile,and the smallest radius of the burst area. Let’s help Sconbin to get the award.

Input

There are many test cases.Each case consists of a positive integer N(N<500,^V^,our great king might be a considerate lover) on a line followed by N lines giving the coordinates of N letters.Each coordinates have two numbers,x coordinate and y coordinate.N=0 is the end of the input file.

Output

For each case,there should be a single line in the output,containing three numbers,the first and second are x and y coordinates of the missile to launch,the third is the smallest radius the missile need to destroy all N letters.All output numbers are rounded to the second digit after the decimal point.

Sample Input

3 1.00 1.00 2.00 2.00 3.00 3.00 0

Sample Output

2.00 2.00 1.41

题解

这道题目的大概意思就是给你n个点,让你求出能覆盖这n个点的最小圆。

这道题目就是经典的最小圆覆盖啦。。我们可以这样来分步解这道题:

第一步:先随机在n个点中选3个点 A,B,C A , B , C (可以用random_shuffle但是我没用)

第二步:可以作一个包含 A,B,C A , B , C 三个点的最小圆,这三个点可以都在圆周上,也可以有两个在圆周上,而后一种情况的那两个点一定是圆的某条半径的两个端点(不然就可以再缩)

第三步:找出离圆心最远的 D D 点,若D点已经在圆内,则结束。否则,执行第四步

第四步:从 A,B,C,D A , B , C , D 中选三个点,使得由它们生成的包含这四个点的圆最小,此三个点形成新的 A,B,C A , B , C 返回执行第2步。如果这一步形成的圆只经过两个点 A,B A , B 那么就从 C,D C , D 中任意选一个点作为第三个点返回第二步即可。

这样,这道题目的思路就出来了。。。不过有一个东西确实有点坑。。。就是怎么求一个三角形的外接圆圆心(至于半径,知道圆心就出来了),网上的很多题解要么是没讲,要么是直接给公式,这里我就来讲一讲:

首先设要求的这个外接圆的圆心为 (x0,y0) ( x 0 , y 0 ) ,半径为 r r ,三个点的坐标为(x13,y13)

我们可以很快的列出这个圆的方程: (xx0)2+(yy0)2=r2 ( x − x 0 ) 2 + ( y − y 0 ) 2 = r 2

然后我们将三个点的坐标带入方程,可以得到三个方程:

(x1x0)2+(y1y0)2=r2 ( x 1 − x 0 ) 2 + ( y 1 − y 0 ) 2 = r 2
(x2x0)2+(y2y0)2=r2 ( x 2 − x 0 ) 2 + ( y 2 − y 0 ) 2 = r 2
(x3x0)2+(y3y0)2=r2 ( x 3 − x 0 ) 2 + ( y 3 − y 0 ) 2 = r 2

大家乍一看,发现,又要解二元方程了啊。。。但是仔细一看,实际上如果把相邻两个相减, x0y0 x 0 和 y 0 的二次项都被消掉了, r2 r 2 项也被消掉了,所以就变成一个二元一次方程组了,这样就好解了。

这道题目就讲完了,接下来放代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 510
using namespace std;
const double eps=1e-10;
int n; 
struct P{
    double x,y;
}a[maxn];
double dist(P p1,P p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
P o;
double r;
void cir(P p1,P p2,P p3)//求外接圆
{
    double a=2*(p2.x-p1.x),b=2*(p2.y-p1.y);
    double c=p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
    double d=2*(p3.x-p1.x),e=2*(p3.y-p1.y);
    double f=p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
    o.x=(b*f-e*c)/(b*d-e*a);
    o.y=(d*c-a*f)/(b*d-e*a);
    r=dist(o,p1);
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)   scanf("%lf%lf",&a[i].x,&a[i].y);
        o=a[1];
        r=0;
        for(int i=2;i<=n;i++)
        {
            if(dist(o,a[i])>r+eps)
            {
                o=a[i];r=0;
                for(int j=1;j<=i-1;j++)
                {
                    if(dist(o,a[j])>r+eps)
                    {
                        o.x=(a[i].x+a[j].x)/(double)2;
                        o.y=(a[i].y+a[j].y)/(double)2;
                        r=dist(o,a[i]);
                        for(int k=1;k<=j-1;k++)
                        {
                            if(dist(o,a[k])>r+eps)  cir(a[i],a[j],a[k]);
                        }
                    }
                }
            }
        }
        printf("%.2lf %.2lf %.2lf\n",o.x,o.y,r);
    }
    return 0;
}

谢谢大家!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值