您的位置:首页 > 其它

HDU 3007 Buried memory(计算几何の最小圆覆盖,模版题)

2013-07-21 22:50 393 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

题目大意:给n个点,求一个最小的圆,使得所有点都在这个圆内
思路:直接套算法(好像是叫三角形增量法,那个好像插入排序的东西貌似只是一个优化,去掉依然AC,时间依然没变(估计数据太小了……))

#include <cstdio>
#include <cmath>

const int MAXN = 1010;

struct Point {
double x, y;
Point(double xx = 0, double yy = 0): x(xx), y(yy) {}
};

double operator ^ (const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
}

Point operator - (const Point &a, const Point &b) {
return Point(a.x - b.x, a.y - b.y);
}

double dist(const Point &a, const Point &b) {
double x = a.x - b.x, y = a.y - b.y;
return sqrt(x * x + y * y);
}

struct Circle {
double r;
Point centre;
};

struct Triangle {
Point t[3];
double Area() {
return fabs((t[1] - t[0]) ^ (t[2] - t[0]))/2;
}
};

Circle circumcircleOfTriangle(Triangle t) {
Circle tmp;
double a, b, c, c1, c2;
Point A(t.t[0].x, t.t[0].y);
Point B(t.t[1].x, t.t[1].y);
Point C(t.t[2].x, t.t[2].y);
a = dist(B, C);
b = dist(A, C);
c = dist(A, B);
tmp.r = a * b * c / t.Area() / 4;
c1 = (A.x * A.x + A.y * A.y - B.x * B.x - B.y * B.y) / 2;
c2 = (A.x * A.x + A.y * A.y - C.x * C.x - C.y * C.y) / 2;
tmp.centre.x = (c1 * (A.y - C.y) - c2 * (A.y - B.y)) / ((A.x - B.x) * (A.y - C.y) - (A.x - C.x) * (A.y - B.y));
tmp.centre.y = (c1 * (A.x - C.x) - c2 * (A.x - B.x)) / ((A.y - B.y) * (A.x - C.x) - (A.y - C.y) * (A.x - B.x));
return tmp;
}

Circle c;
Point a[MAXN];

Circle MinCircle2(int tce, Triangle ce) {
Circle tmp;
if(tce == 0) tmp.r = -2;
else if(tce == 1) {
tmp.centre = ce.t[0];
tmp.r = 0;
}
else if(tce == 2) {
tmp.r = dist(ce.t[0], ce.t[1]) / 2;
tmp.centre.x = (ce.t[0].x + ce.t[1].x) / 2;
tmp.centre.y = (ce.t[0].y + ce.t[1].y) / 2;
}
else if(tce == 3) tmp = circumcircleOfTriangle(ce);
return tmp;
}

void MinCircle(int t, int tce, Triangle ce) {
Point tmp;
c = MinCircle2(tce, ce);
if(tce == 3) return;
for(int i = 1; i <= t; ++i) {
if(dist(a[i], c.centre) > c.r) {
ce.t[tce] = a[i];
MinCircle(i - 1, tce + 1, ce);
tmp = a[i];
for(int j = i; j >= 2; --j) a[j] = a[j - 1];
a[1] = tmp;
}
}
}

void run(int n) {
Triangle ce;
MinCircle(n, 0, ce);
printf("%.2f %.2f %.2f\n", c.centre.x, c.centre.y, c.r);
}

int main() {
int n;
while(scanf("%d", &n) != EOF) {
if(n == 0) break;
for(int i = 1; i <= n; ++i)
scanf("%lf%lf", &a[i].x, &a[i].y);
run(n);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: