hdoj4720Naive and Silly Muggles【求外接圆圆心】



Naive and Silly Muggles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 858    Accepted Submission(s): 557


Problem Description
Three wizards are doing a experiment. To avoid from bothering, a special magic is set around them. The magic forms a circle, which covers those three wizards, in other words, all of them are inside or on the border of the circle. And due to save the magic power, circle's area should as smaller as it could be.
Naive and silly "muggles"(who have no talents in magic) should absolutely not get into the circle, nor even on its border, or they will be in danger.
Given the position of a muggle, is he safe, or in serious danger?
 

Input
The first line has a number T (T <= 10) , indicating the number of test cases.
For each test case there are four lines. Three lines come each with two integers x i and y i (|x i, y i| <= 10), indicating the three wizards' positions. Then a single line with two numbers q x and q y (|q x, q y| <= 10), indicating the muggle's position.
 

Output
For test case X, output "Case #X: " first, then output "Danger" or "Safe".
 

Sample Input
      
      
3 0 0 2 0 1 2 1 -0.5 0 0 2 0 1 2 1 -0.6 0 0 3 0 1 1 1 -1.5
 

Sample Output
      
      
Case #1: Danger Case #2: Safe Case #3: Safe
 

题意:用最小的一个圆包围三个点判断一点是否在该圆内欲使该圆最小圆心无非在三个位置任意两条边的中点三角形外接圆圆心;求出半径最小的圆心和该点比较即可

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define inf 0x3f3f3f3f
using namespace std;
struct point{
	double x,y;
}A[3];
int sgn(double n){
	if(fabs(n)<eps)return 0;
	if(n<0)return -1;
	return 1;
}
double MIN(double a,double b){
	return a<b?a:b;
}
double MAX(double a,double b){
	return a>b?a:b;
}
double dist(point p1,point p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
void getcircle(point p1,point p2,point p3,point &circle){//求外接圆圆心
	double Bx=p2.x-p1.x,By=p2.y-p1.y;
	double Cx=p3.x-p1.x,Cy=p3.y-p1.y;
	double D=2.0*(Bx*Cy-By*Cx);
	circle.x=(Cy*(Bx*Bx+By*By)-By*(Cx*Cx+Cy*Cy))/D+p1.x;
	circle.y=(Bx*(Cx*Cx+Cy*Cy)-Cx*(Bx*Bx+By*By))/D+p1.y;
}
int main()
{
	int t,i,j,k=1;
	point muggle;
	scanf("%d",&t);
	while(t--){
		for(i=0;i<3;++i){
			scanf("%lf%lf",&A[i].x,&A[i].y);
		}
		scanf("%lf%lf",&muggle.x,&muggle.y);
		point circle,circle1;double r=inf;
		for(i=0;i<3;++i){
			circle1.x=(A[i].x+A[(i+1)%3].x)/2.0;
			circle1.y=(A[i].y+A[(i+1)%3].y)/2.0;
			double temp=MAX(dist(circle1,A[0]),MAX(dist(circle1,A[1]),dist(circle1,A[2])));
			if(temp<r){
				r=temp;
				circle=circle1;
			}
		}
		if(sgn((A[2].x-A[0].x)*(A[1].y-A[0].y)-(A[2].y-A[0].y)*(A[1].x-A[0].x))!=0){//判断三点共线
			getcircle(A[0],A[1],A[2],circle1);
			double temp=MAX(dist(circle1,A[0]),MAX(dist(circle1,A[1]),dist(circle1,A[2])));
			if(temp<r){
				r=temp;
				circle=circle1;
			}
		}
		printf("Case #%d: ",k++);
		if(sgn(r-dist(muggle,circle))>=0)
			printf("Danger\n");
		else 
			printf("Safe\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值