Naive and Silly MugglesTime 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
Sample Output
|
题意:用最小的一个圆包围三个点判断一点是否在该圆内欲使该圆最小圆心无非在三个位置任意两条边的中点三角形外接圆圆心;求出半径最小的圆心和该点比较即可
#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;
}