HDU 3007(最小圆覆盖)

HDU 3007(最小圆覆盖)

问题描述:

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 < 500,求最小圆覆盖

算法思路 :

常见两种算法 : 几何算法,模拟退火

1 . 几何算法

一个最小圆可以由两个点或者三个点来确定;两个点是圆心就是线段AB的中点,三个点的圆心就是三角形的外心;

增量法 :

  1. 先加入一个点p1,然后c1的圆心就是p1,半径为0;

  2. 加入第二个点p2,然后c2的圆心就是线段p1,p2的中心,半径为两点距离的一半;

  3. 加入第三个点p3;有两种情况 :

    p3在c2的内部或者圆上,不影响原来的最小圆;

    p3在c2的外部,此时需要更新;因为p3一定需要在c3上,现在的任务就是在p1,p2中找到一个点或者两个点,与p3一起两点定圆或者三点定圆;定圆的过程回到1,然后依次加入p1,p2;

    算法复杂度接近O(n);

#include <bits/stdc++.h>

using namespace std;
const double eps = 1e-8;//偏差值
const int maxn = 505;//点的数量

int sgn(double x) {
    if (fabs(x) < eps)return 0;
    else return x < 0 ? -1 : 1;
}
struct Point {
    double x, y;
};
double Distance(Point A, Point B) { return hypot(A.x - B.x, A.y - B.y);}

//求三角形abc的外接圆的圆心
Point circle_center(const Point a,const Point b,const Point c){
    Point center;
    double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2;
    double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2;
    double d = a1 * b2 - a2 * b1;
    center.x = a.x + (c1 * b2 - c2 * b1) / d;
    center.y = a.y + (a1 * c2 - a2 * c1) / d;
    return center;
}

//求最小覆盖圆,返回圆心c,半径r
void min_cover_circle(Point *p,int n,Point &c,double &r){
    random_shuffle(p, p + n); //随机函数,打乱所有点
    c = p[0];r = 0; //从第一个点p0开始,圆心为p0,半径为0
    for (int i = 1; i < n;i++){ //扩展所有点
        if(sgn(Distance(p[i],c) - r) > 0){ //点pi在圆外
            c = p[i];r = 0; //重新设置圆心为pi,半径为0
            for (int j = 0; j < i;j ++){ //重新检测前面所有点
                if(sgn(Distance(p[j],c) - r) > 0){ //两点定圆
                    c.x = (p[i].x + p[j].x) / 2;
                    c.y = (p[i].y + p[j].y) / 2;
                    r = Distance(p[j], c);
                    for (int k = 0; k < j;k ++){
                        if(sgn(Distance(p[k],c) - r) > 0){ //两点定圆不行就是三点定圆
                            c = circle_center(p[i], p[j], p[k]);
                            r = Distance(p[i], c);
                        }
                    }
                }
            }
        }
    }
}

int main(){
    int n;
    Point p[maxn];
    Point c;double r;
    while(~scanf("%d", & n) && n){
        for (int i = 0; i < n;i ++) scanf("%lf %lf",&p[i].x,&p[i].y);
        min_cover_circle(p, n, c, r);
        printf("%.2f %.2f %.2f\n", c.x, c.y, r);
    }
    return 0;
}
2 . 模拟退火算法

用模拟退火求最小圆,不断迭代降温;在每次迭代时,找到能覆盖到所有点的一个圆;在多次迭代中,逐步逼近最后要求的圆心和半径;

#include <bits/stdc++.h>

using namespace std;
const double eps = 1e-8;//偏差值
const int maxn = 505;//点的数量

int sgn(double x) {
 if (fabs(x) < eps)return 0;
 else return x < 0 ? -1 : 1;
}
struct Point {
 double x, y;
};
double Distance(Point A, Point B) { return hypot(A.x - B.x, A.y - B.y);}

//求三角形abc的外接圆的圆心
Point circle_center(const Point a,const Point b,const Point c){
 Point center;
 double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2;
 double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2;
 double d = a1 * b2 - a2 * b1;
 center.x = a.x + (c1 * b2 - c2 * b1) / d;
 center.y = a.y + (a1 * c2 - a2 * c1) / d;
 return center;
}

//求最小覆盖圆,返回圆心c,半径r
void min_cover_circle(Point *p,int n,Point &c,double &r){
 double T = 100.0;
 double delta = 0.98;
 c = p[0];
 int pos;
 while(T > eps){
     pos = 0;r = 0;
     for (int i = 0; i <= n - 1;i ++){
         if(Distance(c,p[i]) > r){
             r = Distance(c, p[i]);
             pos = i;
         }
     }
     c.x += (p[pos].x - c.x) / r * T;
     c.y += (p[pos].y - c.y) / r * T;
     T *= delta;
 }
}

int main(){
 int n;
 Point p[maxn];
 Point c;double r;
 while(~scanf("%d", & n) && n){
     for (int i = 0; i < n;i ++) scanf("%lf %lf",&p[i].x,&p[i].y);
     min_cover_circle(p, n, c, r);
     printf("%.2f %.2f %.2f\n", c.x, c.y, r);
 }
 return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值