hdu-3007(计算几何+最小覆盖圆)
2017-09-03 10:09
381 查看
问题描述:
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
Sample Output
题目题意:题目给我们n个点的坐标,让我们求一个最小圆(可以覆盖所有点)的圆心坐标和半径。
题目分析:题目就一个经典问题,最小圆覆盖问题。
学习了大牛的博客:点击打开链接和点击打开链接。
模板可以自己保存下来咯!
代码如下:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
struct Point
{
double x,y;
};
struct Point a[1005],d;
double r;
double get_dis(Point p1,Point p2) //两点间距离
{
return (sqrt((p1.x-p2.x)*(p1.x -p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
double get_muti(Point p1, Point p2,Point p0)
{
return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}
void get_o(Point p,Point q,int n)
{
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;
r=get_dis(p,q)/2;
int k;
double c1,c2,t1,t2,t3;
for(k=1;k<=n;k++) {
if(get_dis(d,a[k])<=r)continue;
if(get_muti(p,q,a[k])!=0.0) {
c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;
c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;
d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));
d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));
r=get_dis(d,a[k])
4000
;
}
else {
t1=get_dis(p,q);
t2=get_dis(q,a[k]);
t3=get_dis(p,a[k]);
if(t1>=t2&&t1>=t3) {
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;r=get_dis(p,q)/2.0;
}
else if(t2>=t1&&t2>=t3) {
d.x=(a[k].x+q.x)/2.0;
d.y=(a[k].y+q.y)/2.0;
r=get_dis(a[k],q)/2.0;
}
else {
d.x=(a[k].x+p.x)/2.0;
d.y=(a[k].y+p.y)/2.0;
r=get_dis(a[k],p)/2.0;
}
}
}
}
void solve(Point pi,int n)
{
d.x=(pi.x+a[1].x)/2.0;
d.y=(pi.y+a[1].y)/2.0;
r=get_dis(pi,a[1])/2.0;
int j;
for(j=2;j<=n;j++){
if(get_dis(d,a[j])<=r)continue;
else
get_o(pi,a[j],j-1);
}
}
int main()
{
int i,n;
while(scanf("%d",&n)&&n){
for(i=1;i<=n;i++){
scanf("%lf %lf",&a[i].x,&a[i].y);
}
if(n==1) { printf("%.2lf %.2lf 0.00\n",a[1].x,a[1].y);continue;}
r=get_dis(a[1],a[2])/2.0;
d.x=(a[1].x+a[2].x)/2.0;
d.y=(a[1].y+a[2].y)/2.0;
for(i=3;i<=n;i++){
if(get_dis(d,a[i])<=r)continue;
else
solve(a[i],i-1);
}
printf("%.2lf %.2lf %.2lf\n",d.x,d.y,r);
}
return 0;
}
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个点的坐标,让我们求一个最小圆(可以覆盖所有点)的圆心坐标和半径。
题目分析:题目就一个经典问题,最小圆覆盖问题。
学习了大牛的博客:点击打开链接和点击打开链接。
模板可以自己保存下来咯!
代码如下:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
struct Point
{
double x,y;
};
struct Point a[1005],d;
double r;
double get_dis(Point p1,Point p2) //两点间距离
{
return (sqrt((p1.x-p2.x)*(p1.x -p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
double get_muti(Point p1, Point p2,Point p0)
{
return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}
void get_o(Point p,Point q,int n)
{
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;
r=get_dis(p,q)/2;
int k;
double c1,c2,t1,t2,t3;
for(k=1;k<=n;k++) {
if(get_dis(d,a[k])<=r)continue;
if(get_muti(p,q,a[k])!=0.0) {
c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;
c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;
d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));
d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));
r=get_dis(d,a[k])
4000
;
}
else {
t1=get_dis(p,q);
t2=get_dis(q,a[k]);
t3=get_dis(p,a[k]);
if(t1>=t2&&t1>=t3) {
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;r=get_dis(p,q)/2.0;
}
else if(t2>=t1&&t2>=t3) {
d.x=(a[k].x+q.x)/2.0;
d.y=(a[k].y+q.y)/2.0;
r=get_dis(a[k],q)/2.0;
}
else {
d.x=(a[k].x+p.x)/2.0;
d.y=(a[k].y+p.y)/2.0;
r=get_dis(a[k],p)/2.0;
}
}
}
}
void solve(Point pi,int n)
{
d.x=(pi.x+a[1].x)/2.0;
d.y=(pi.y+a[1].y)/2.0;
r=get_dis(pi,a[1])/2.0;
int j;
for(j=2;j<=n;j++){
if(get_dis(d,a[j])<=r)continue;
else
get_o(pi,a[j],j-1);
}
}
int main()
{
int i,n;
while(scanf("%d",&n)&&n){
for(i=1;i<=n;i++){
scanf("%lf %lf",&a[i].x,&a[i].y);
}
if(n==1) { printf("%.2lf %.2lf 0.00\n",a[1].x,a[1].y);continue;}
r=get_dis(a[1],a[2])/2.0;
d.x=(a[1].x+a[2].x)/2.0;
d.y=(a[1].y+a[2].y)/2.0;
for(i=3;i<=n;i++){
if(get_dis(d,a[i])<=r)continue;
else
solve(a[i],i-1);
}
printf("%.2lf %.2lf %.2lf\n",d.x,d.y,r);
}
return 0;
}
相关文章推荐
- HDU 3007 最小圆覆盖 计算几何
- HDU 3007 Buried memory(计算几何の最小圆覆盖,模版题)
- HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)
- HDU 5251 矩形面积 (计算几何+旋转卡壳求覆盖凸包的最小矩形面积)
- HDU 3932(计算几何+最小圆覆盖)
- HDU 4720(计算几何+最小圆覆盖)
- hdu 4606 Occupy Cities - 计算几何 + 最短路 + 最小路径覆盖
- 【BZOJ1185】最小矩形覆盖 计算几何 凸包 旋转卡壳
- HDU4606 Occupy Cities 计算几何+最小路径覆盖
- HDU4606 Occupy Cities 计算几何+最小路径覆盖
- hdu 3007(最小圆覆盖)
- ZOJ 1450 HDU 3007 (最小圆覆盖)
- hdu 3007 Buried memory (最小覆盖圆)
- bzoj1185: [HNOI2007]最小矩形覆盖 计算几何 旋转卡壳
- hdu 3548 最小三角形周长(计算几何)
- HDU 3007 Buried memory(点集最小圆覆盖 模拟退火解法)
- hdu 3007 Buried memory (最小圆覆盖)
- hdu3007Buried memory 最小圆覆盖 计算几何
- bzoj1337 最小圆覆盖 计算几何 解题报告
- hdu4606 Occupy Citie 简单计算几何,最小路径覆盖