Naive and Silly Muggles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 136 Accepted Submission(s): 93
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?
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 xi and yi (|xi, yi| <= 10), indicating the three wizards' positions. Then a single line with two numbers qx and qy (|qx, qy| <= 10), indicating the muggle's position.
For each test case there are four lines. Three lines come each with two integers xi and yi (|xi, yi| <= 10), indicating the three wizards' positions. Then a single line with two numbers qx and qy (|qx, qy| <= 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
Source
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <fstream> #include <vector> #include <set> #include <queue> #include <map> #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #pragma comment(linker, "/STACK:16777216") #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) using namespace std ; typedef long long LL ; const int size=100008 ; struct Point{ double x; double y; }pt[1005]; struct Traingle{ struct Point p[3]; }; struct Circle{ struct Point center; double r; }ans; //计算两点距离 double Dis(struct Point p, struct Point q){ double dx=p.x-q.x; double dy=p.y-q.y; return sqrt(dx*dx+dy*dy); } //计算三角形面积 double Area(struct Traingle ct){ return fabs((ct.p[1].x-ct.p[0].x)*(ct.p[2].y-ct.p[0].y)-(ct.p[2].x-ct.p[0].x)*(ct.p[1].y-ct.p[0].y))/2.0; } //求三角形的外接圆,返回圆心和半径 (存在结构体"圆"中) struct Circle CircumCircle(struct Traingle t){ struct Circle tmp; double a, b, c, c1, c2; double xA, yA, xB, yB, xC, yC; a = Dis(t.p[0], t.p[1]); b = Dis(t.p[1], t.p[2]); c = Dis(t.p[2], t.p[0]); //根据 S = a * b * c / R / 4;求半径 R tmp.r = (a*b*c)/(Area(t)*4.0); xA = t.p[0].x; yA = t.p[0].y; xB = t.p[1].x; yB = t.p[1].y; xC = t.p[2].x; yC = t.p[2].y; c1 = (xA*xA+yA*yA - xB*xB-yB*yB) / 2; c2 = (xA*xA+yA*yA - xC*xC-yC*yC) / 2; tmp.center.x = (c1*(yA - yC)-c2*(yA - yB)) / ((xA - xB)*(yA - yC)-(xA - xC)*(yA - yB)); tmp.center.y = (c1*(xA - xC)-c2*(xA - xB)) / ((yA - yB)*(xA - xC)-(yA - yC)*(xA - xB)); return tmp; } double xmult(Point p1,Point p2,Point p0){ return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } int dots_inline(Point p1,Point p2,Point p3){ return zero(xmult(p1,p2,p3)); } int gao(Point A, Point B ,Point C,Circle &me){ double x=0.5*(A.x+B.x) ; double y=0.5*(A.y+B.y) ; double R=0.5*Dis(A,B) ; if((C.x-x)*(C.x-x)+(C.y-y)*(C.y-y)<=R*R){ me.center.x=x ; me.center.y=y ; me.r=R ; return 1 ; } return 0; } int main(){ int T ,k=1; double x , y ; cin>>T ; while(T--){ Traingle tra ; cin>>tra.p[0].x>>tra.p[0].y ; cin>>tra.p[1].x>>tra.p[1].y ; cin>>tra.p[2].x>>tra.p[2].y ; cin>>x>>y ; Circle C; C.r=10000000.0 ; Circle me ; if(gao(tra.p[0],tra.p[1],tra.p[2],me)){ if(C.r>me.r) C=me ; } if(gao(tra.p[0],tra.p[2],tra.p[1],me)){ if(C.r>me.r) C=me ; } if(gao(tra.p[1],tra.p[2],tra.p[0],me)){ if(C.r>me.r) C=me ; } if(dots_inline(tra.p[0],tra.p[1],tra.p[2])){} else me=CircumCircle(tra) ; if(C.r>me.r) C=me ; if((x-C.center.x)*(x-C.center.x)+(y-C.center.y)*(y-C.center.y)<=C.r*C.r) printf("Case #%d: Danger\n",k++) ; else printf("Case #%d: Safe\n",k++) ; } return 0 ; }