四川省ACM竞赛(2013)---E - Naive and Silly Muggles

E - Naive and Silly Muggles
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
Submit Status

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
 



AC CODE:
#include <iostream>
#include <math.h>
using namespace std;
#define eps 1e-8
int main()
{
    int T,i;
    double x1,y1,x2,y2,x3,y3,x4,y4;
    double x,y,x0,y0;
    double a,b,c,d,e,f,r,r0;
    double result,result0;
    double d0,d1,d2,d3;
    cin>>T;
    for(i=1;i<=T;i++)
    {	
		cout<<"Case #"<<i<<": ";
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        a=2*(x2-x1);
        b=2*(y2-y1);
        c=x2*x2+y2*y2-x1*x1-y1*y1;
        d=2*(x3-x2);
        e=2*(y3-y2);
        f=x3*x3+y3*y3-x2*x2-y2*y2;
        x=(b*f-e*c)/(b*d-e*a);
        y=(d*c-a*f)/(b*d-e*a);
        r=(x-x1)*(x-x1)+(y-y1)*(y-y1);
        d1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        d2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
        d3=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
        if(d1>=d2&&d1>=d3)
        {
            x0=(x1+x2)/2;
            y0=(y1+y2)/2;
            r0=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
            result0=(x3-x0)*(x3-x0)+(y3-y0)*(y3-y0);
            if(result0-r0<=eps&&r0-r<=eps)
            {
                r=r0;
                x=x0;
                y=y0;
            }
        }
        else if(d2>=d1&&d2>=d3)
        {
            x0=(x1+x3)/2;
            y0=(y1+y3)/2;
            r0=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
            result0=(x2-x0)*(x2-x0)+(y2-y0)*(y2-y0);
            if(result0-r0<=eps&&r0-r<=eps)
            {
                r=r0;
                x=x0;
                y=y0;
            }
        }
        else if(d3>=d1&&d3>=d2)
        {
            x0=(x2+x3)/2;
            y0=(y2+y3)/2;
            r0=(x2-x0)*(x2-x0)+(y2-y0)*(y2-y0);
            result0=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
            if(result0-r0<=eps&&r0-r<=eps)
            {
                r=r0;
                x=x0;
                y=y0;
            }
        }
		result=(x4-x)*(x4-x)+(y4-y)*(y4-y);
       // cout<<x0<<" "<<y0<<" "<<r0<<endl;
        //cout<<"x:"<<x<<" "<<"y:"<<y<<" "<<"r:"<<r<<" "<<"result"<<result<<endl;
	    //result=(x4-x)*(x4-x)+(y4-y)*(y4-y);
        if(result-r<=eps)
            cout<<"Danger"<<endl;
        else
            cout<<"Safe"<<endl;
    }
    return 0;
}

纯简单几何计算题,只需考虑三点成钝角三角形的特殊情况就行了。。。

你可能感兴趣的:(C++)