ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)

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

题目大意就是先求一个能包含三个点的最小圆,然后判断第四个圆是否在圆内。

这三点中取出两点,如果以这两个点构成的线段为直径,能包含第三个点,自然便是最小圆。于是先考虑最远的两个点即可。

其次,如果上述不满足(三点一线的满足上面),自然需要逐步扩大直径来包含第三个点,自然所求的便是外接圆。

对于求外接圆,此处采用了暴力设圆心坐标(x, y)

所以(x-x1)^2 + (y-y1)^2 = (x-x2)^2 + (y-y2)^2 = (x-x3)^2 + (y-y3)^2

化简得到:

2*((x1-x2)*(y1-y3) - (x1-x3)*(y1-y2)) * x

= (y1-y2)*(y2-y3)*(y1-y3) + (x1*x1-x2*x2)*(y1-y3) - (x1*x1-x3*x3)*(y1-y2);

2*((y1-y2)*(x1-x3) - (y1-y3)*(x1-x2)) * y

= (x1-x2)*(x2-x3)*(x1-x3) + (y1*y1-y2*y2)*(x1-x3) - (y1*y1-y3*y3)*(x1-x2);

于是圆心求出来问题便简单了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#define LL long long

using namespace std;

double x1,x2,x3,y1,y2,y3, x0, y0;
double rx, ry, r2;
int  n,i;

void Cal()
{
    double A, B;
    A = 2*((x1-x2)*(y1-y3) - (x1-x3)*(y1-y2));
    B = (y1-y2)*(y2-y3)*(y1-y3) + (x1*x1-x2*x2)*(y1-y3) - (x1*x1-x3*x3)*(y1-y2);
    rx = B/A;

    A = 2*((y1-y2)*(x1-x3) - (y1-y3)*(x1-x2));
    B = (x1-x2)*(x2-x3)*(x1-x3) + (y1*y1-y2*y2)*(x1-x3) - (y1*y1-y3*y3)*(x1-x2);
    ry = B/A;
    r2 = (rx-x1)*(rx-x1) + (ry-y1)*(ry-y1);
}

void Work()
{
    int cnt = 3;
    double tmp;
    r2 = ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))/4;
    rx = (x1+x2)/2;
    ry = (y1+y2)/2;
    tmp = ((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3))/4;
    if (tmp > r2)
    {
        cnt = 1;
        r2 = tmp;
        rx = (x3+x2)/2;
        ry = (y3+y2)/2;
    }
    tmp = ((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3))/4;
    if (tmp > r2)
    {
        cnt = 2;
        r2 = tmp;
        rx = (x1+x3)/2;
        ry = (y1+y3)/2;
    }
    switch (cnt)
    {
        case 1:
            tmp = (rx-x1)*(rx-x1) + (ry-y1)*(ry-y1);
            break;
        case 2:
            tmp = (rx-x2)*(rx-x2) + (ry-y2)*(ry-y2);
            break;
        case 3:
            tmp = (rx-x3)*(rx-x3) + (ry-y3)*(ry-y3);
            break;
    }
    if (tmp > r2)
    {
        Cal();
    }
}

void Output()
{
    if (r2 >= (rx-x0)*(rx-x0) + (ry-y0)*(ry-y0))
        printf("Danger\n");
    else
        printf("Safe\n");
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for(int times = 1; times <= T; times++)
    {
        scanf("%lf%lf", &x1, &y1);
        scanf("%lf%lf", &x2, &y2);
        scanf("%lf%lf", &x3, &y3);
        scanf("%lf%lf", &x0, &y0);
        Work();
        printf("Case #%d: ", times);
        Output();
    }
}
时间: 2024-12-17 03:41:28

ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)的相关文章

Naive and Silly Muggles(计算几何 判断点是否在三点形成的最小圆内)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4720 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

ACM学习历程—FZU 2144 Shooting Game(计算几何 &amp;&amp; 贪心 &amp;&amp; 排序)

Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.) But as they don’t like using repellent while playing this kind of special (hent

ACM学习历程—HDU1392 Surround the Trees(计算几何)

Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?        The

HDOJ 4720 Naive and Silly Muggles 三角形外接圆

当是钝角三角形时,就是最长边为直径的圆最小. 否则,求三角形的外接圆 Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 700    Accepted Submission(s): 451 Problem Description Three wizards are doing a exper

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

计算几何 HDOJ 4720 Naive and Silly Muggles

题目传送门 /* 题意:给三个点求它们的外接圆,判断一个点是否在园内 计算几何:我用重心当圆心竟然AC了,数据真水:) 正解以后补充,http://www.cnblogs.com/kuangbin/archive/2013/09/11/3315055.html */ #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <string

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

HDU 4720 :Naive and Silly Muggles

题目:HDU 4720 :Naive and Silly Muggles 题目大意:这题的意思是给出三个点, 然后在给出另一个点,问这个点会不会在覆盖前面三个点的最小的圆里面(包括边界), 在里面最输出danger, 如果任何情况下这个点都不在圆里面,那么就输出safe. 解题思路:三个点最小的覆盖的圆是三角形的外接圆,这样的圆面积一定是最小的. 但是相同面积的圆,所在的位置,覆盖的点会是不一样的.例如垂心关于三条边的对称点,以某个对称点为圆心的圆用之前的半径做圆,如果能够覆盖原来的三个点(点可

hdu 4720 Naive and Silly Muggles(几何)

题目链接:hdu 4720 Naive and Silly Muggles 题目大意:给出三点,找出一个圆,要求面积尽量小,并且三点必须在园内,如果可以找到一个圆,使得说第4个点不在圆内则是安全的. 解题思路:面积最小即三个点外切圆,根据三角形两条边的垂直平分线求出圆心.判断第4个点是否在圆内只要计算距离即可. 然后还要考虑说面积和外切圆相同,但是圆心不同的圆. #include <cstdio> #include <cstring> #include <cmath>