본문 바로가기
[C, C++ 알고리즘]

[백준 1002] 터렛

by BeNev0L 2021. 11. 8.

터렛의 위치와 터렛과 마린사이의 거리가 주어졌다.

터렛의 위치를 원의 중심, 마린은 두 원의 접점이라고 보면 된다.

쉽죠?

 

근데 처음에는 겹치는 넓이 안에 있는거 다 구해야하는 줄 알았음 ㅋㅋ;

 

두 원의 위치관계는 중학교 때 배운 것 같은데, 그거 쓰면 된다.

 

두 원의 중심 사이의 거리를 d, 두 원의 반지름을 각각 r, r' 라고 하면,

출처 : 수학방(https://mathbang.net/101)

요로코롬 된다는 걸 알 수 있다.

 

<코드>

#include <iostream>
#include <cmath>

using namespace std;

typedef long long ll;


int main()
{
    int t;
    cin >> t;
    for(t; t--; t>0){
        ll x1, y1, r1, x2, y2, r2;
        cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
        ll dx = x1 - x2;
        ll dy = y1 - y2;

        if(r1 > r2) swap(r1,  r2);
        ll add = r1 + r2;
        add = pow(add, 2);
        ll sub = r2 - r1;
        sub = pow(sub, 2);
        ll d = pow(dx, 2) + pow(dy, 2);

        if(d < add && d > sub)
            cout << 2;
        else if (d == add || (d == sub && d != 0))
            cout << 1;
        else if (d <sub || d > add)
            cout << 0;
        else if (d == 0){
            if(r1 == r2)
                cout << -1;
            else
                cout << 0;
        }
        cout << '\n';
    }

    return 0;
}

끝.

'[C, C++ 알고리즘]' 카테고리의 다른 글

All pairs Shortest path Problem - Floyd Warshall Algorithm.  (0) 2021.12.10
[백준 1010] 다리 놓기  (0) 2021.11.26
[백준 1009] 분산처리  (0) 2021.11.15
[백준 1005] ACM CRAFT  (0) 2021.11.13
[백준 1004] 어린 왕자  (0) 2021.11.10