昨天用vim练了一道大水题,今天特地找了道稍难一点的题。不过也不是很难,简单的计算几何而已。练习用vim编码,用gdb调试,结果居然1A了,没调试。。。囧。。。
做法很简单,无非就是两种情况:①三个巫师构成一个钝角(极限情况是直角)三角形,那么所画的圆应该是钝角所对边为直径的圆;②三个巫师构成一个锐角三角形,那么所画的圆应该是三角形的外接圆。
就这样纸,上了点模板,代码如下:
/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef struct MyPoint {
double x;
double y;
MyPoint(double xx, double yy) {
x = xx;
y = yy;
}
MyPoint() {
x = y = ;
}
}MyPoint; typedef struct MyLine {
MyPoint a, b;
} MyLine;
MyPoint intersection(MyLine u, MyLine v) {
MyPoint ret = u.a;
double t = ((u.a.x - v.a.x) * (v.a.y - v.b.y)
- (u.a.y - v.a.y) * (v.a.x - v.b.x))
/ ((u.a.x - u.b.x) * (v.a.y - v.b.y)
- (u.a.y - u.b.y) * (v.a.x - v.b.x));
ret.x += (u.b.x - u.a.x) * t;
ret.y += (u.b.y - u.a.y) * t;
return ret;
}
//外心
MyPoint circumcenter(MyPoint a, MyPoint b, MyPoint c) {
MyLine u, v;
u.a.x = (a.x + b.x) / ;
u.a.y = (a.y + b.y) / ;
u.b.x = u.a.x - a.y + b.y;
u.b.y = u.a.y + a.x - b.x;
v.a.x = (a.x + c.x) / ;
v.a.y = (a.y + c.y) / ;
v.b.x = v.a.x - a.y + c.y;
v.b.y = v.a.y + a.x - c.x;
return intersection(u, v);
}
typedef struct MyCircle {
MyPoint p;
double r;
}MyCircle; inline double dis(const MyPoint &p1, const MyPoint &p2) {
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
} MyCircle getCircle(MyPoint ps[]) {
MyCircle ret;
ret.r = -;
for(int i = ; i < ; i++) {
MyPoint a = ps[i % ];
MyPoint b = ps[(i + ) % ];
MyPoint c = ps[(i + ) % ];
double x = (a.x + b.x) / ;
double y = (a.y + b.y) / ;
MyPoint t = MyPoint(x, y);
double r = dis(a, b) / ;
double temp = dis(t, c);
if(temp <= r) {
ret.r = r;
ret.p = t;
}
}
if(ret.r != -) {
return ret;
}
ret.p = circumcenter(ps[], ps[], ps[]);
ret.r = dis(ret.p, ps[]);
return ret;
} int main() {
// freopen("data.in", "r", stdin);
int T;
double x, y;
scanf("%d", &T);
MyPoint wizards[];
MyPoint muggle;
for(int t = ; t <= T; t++) {
for(int i = ; i < ; i++) {
scanf("%lf%lf", &x, &y);
wizards[i] = MyPoint(x, y);
}
scanf("%lf%lf", &x, &y);
muggle = MyPoint(x, y);
MyCircle c = getCircle(wizards);
double d = dis(muggle, c.p);
if(d <= c.r) {
printf("Case #%d: Danger\n", t);
} else {
printf("Case #%d: Safe\n", t);
}
}
return ;
}