HDU 3007:Buried memory

来源:互联网 发布:女儿出嫁父亲感人知乎 编辑:程序博客网 时间:2024/04/27 23:56

Buried memory

 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
31.00 1.002.00 2.003.00 3.000
Sample Output
2.00 2.00 1.41


思路:

1.开始以某一个点为圆心,半径为0,判断其它点是否在这个圆内。如果其它点都在圆内。结束。

2.如果有一个点不在圆内,则以圆心和这个点连线的中点为圆心,连线长度的一半为半径,判断其它点是否在圆内。如果其它点都在圆内。结束

3.如果有一个点不在圆内,则以这三点求出这三点构成的三角形的外接圆的圆心,圆心到其中一点的距离为半径,判断其它点是否在圆内。

这样必然有解。


#include<bits/stdc++.h>using namespace std;int n;struct lenka{double x,y;double r;}a[1000];double dis(const lenka& x,const lenka& y){return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}int main(){while(cin>>n&&n){lenka ans;for(int i=0;i<n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);if(n==1){printf("%0.2f %0.2f 0.00\n",a[0].x,a[0].y);continue;}ans.x=a[0].x,ans.y=a[0].y,ans.r=0;for(int i=1;i<n;i++){if(dis(ans,a[i])<=ans.r)continue;ans=a[i];ans.r=0;for(int j=0;j<i;j++){if(dis(ans,a[j])<=ans.r)continue;ans.x=(ans.x+a[j].x)/2;ans.y=(ans.y+a[j].y)/2;ans.r=dis(ans,a[j]);for(int k=0;k<j;k++){if(dis(a[k],ans)<=ans.r)continue;double x1=a[i].x,y1=a[i].y;double x2=a[j].x,y2=a[j].y;double x3=a[k].x,y3=a[k].y;//三点外接圆的圆心公式 ans.x=( (x1*x1-x2*x2+y1*y1-y2*y2)*(y1-y3)-(x1*x1-x3*x3+y1*y1-y3*y3)*(y1-y2) ) / (2*(y1-y3)*(x1-x2)-2*(y1-y2)*(x1-x3));ans.y=( (x1*x1-x2*x2+y1*y1-y2*y2)*(x1-x3)-(x1*x1-x3*x3+y1*y1-y3*y3)*(x1-x2) ) / (2*(y1-y2)*(x1-x3)-2*(y1-y3)*(x1-x2));ans.r=dis(ans,a[k]);}}}printf("%0.2f %0.2f %0.2f\n",ans.x,ans.y,ans.r);}return 0;}



原创粉丝点击