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
3
1.00 1.00
2.00 2.00
3.00 3.00
0
Sample Output
2.00 2.00 1.41
昨天想刷刷模拟退火的题目呢,就看到这个题了。不清楚这个题用模拟退火行不行,不过不用也可以。
想要找个圆心和半径用把所有的炸弹都引爆。
假如只有一个点,我们直接在那个点上引爆就行。
假如有两个点,我们就在这两点连线的中点引爆,半径是连线距离的一半。
假如有三个点,我们就在三点形成三角形的外接圆的圆心引爆,半径是圆心距离任意一点的距离。
假设点数打于三,我们就重复上面的过程,总会有某几个点形成的连线或者三角形可以包围所有的点。这样就把圆心和半径找出来了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e3+100;
struct node{
	double x,y;
	double r;
}a[maxx];
int n;

inline void read()
{
	for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
}
inline double dis(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline void solve()
{
	node ans=a[1];ans.r=0;
	for(int i=2;i<=n;i++)
	{
		if(dis(ans,a[i])<=ans.r) continue;
		ans=a[i];ans.r=0.0;
		for(int j=1;j<i;j++)
		{
			if(dis(ans,a[j])<=ans.r) continue;
			ans.x=(ans.x+a[j].x)/2.0;
			ans.y=(ans.y+a[j].y)/2.0;
			ans.r=dis(ans,a[j]);//在条件一不符合的时候,就找之前走过的点和这点的连线中点。
			for(int k=1;k<j;k++)
			{
				if(dis(ans,a[k])<=ans.r) continue;
				double x1=a[i].x,x2=a[j].x,x3=a[k].x;//如果条件2不符合的话,就找之前走过的三个点形成的三角形找圆心。
				double y1=a[i].y,y2=a[j].y,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.2lf %0.2lf %0.2lf\n",ans.x,ans.y,ans.r);
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
		read();
		solve();
	}
	return 0;
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值