728x90
알고리즘 분류: 기하학, 수학
문제 링크: www.acmicpc.net/problem/1002
1002번: 터렛
각 테스트 케이스마다 류재명이 있을 수 있는 위치의 수를 출력한다. 만약 류재명이 있을 수 있는 위치의 개수가 무한대일 경우에는 -1을 출력한다.
www.acmicpc.net
두 원의 6가지 관계를 고려한다.
[경우의 수]
1. 일치: -1
2. 멀리 떨어짐: 0
3. 외접: 1
4. 내접: 1
5. 두점: 2
6. 큰 원 내부에 작은 원이 있고 겹치지 않음: 0
총 6가지 경우의 수로 나누어 푼다.
5, 6번 처리가 실수하기 쉬우므로 주의할 것!
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <climits>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define ll long long
#define INF 1e9
using namespace std;
//https://www.acmicpc.net/problem/1002 터렛
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
int x1, y1, r1, x2, y2, r2;
cin >> t;
while(t--) {
cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
//첫번째에 반지름이 더 작은 것으로 저장
if(r2 < r1) {
swap(x1, x2);
swap(y1, y2);
swap(r1, r2);
}
double dist = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
//1. 일치
if(x1 == x2 && y1 == y2 && r1 == r2) cout << -1;
//2. 멀리 떨어져있을 때
else if(r1+r2 < dist) cout << 0;
//3. 외접
else if(r1+r2 == dist) cout << 1;
//4. 내접
else if(dist+r1 == r2) cout << 1;
//5. 두 점
else if(r1+r2 > dist && dist > r2-r1) cout << 2;
//6. 큰 원안에 작은 원이고 겹치지 않을 때
else if(r2-r1 > dist) cout << 0;
cout << '\n';
}
return 0;
}
728x90
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준 / BOJ] 2231 분해합 (0) | 2021.03.16 |
---|---|
[백준 / BOJ] 2798 블랙잭 (0) | 2021.03.16 |
[백준 / BOJ] 2579 계단 오르기 (0) | 2021.03.15 |
[백준 / BOJ] 15650 N과 M(2) (0) | 2021.03.15 |
[백준 / BOJ] 2630 색종이 만들기 (0) | 2021.03.15 |