Data Structure
[Algorithm] Baekjoon - 1002 문제 풀이
Baekjoon Online Judge 1002번 문제 풀이입니다. 이 문제에서는 터렛 2기의 좌표와 각 터렛에서 계산한 적과의 거리를 줍니다. (x1, y1, r1, x2, y2, r2) 문제 풀이 방향을 생각해보면 몇가지 경우의 수가 나옵니다. 우선 문제 풀이 방향은 크기가 같거나
Baekjoon Online Judge
Baekjoon Online Judge 1002번 문제 풀이입니다.
이 문제에서는 터렛 2기의 좌표와 각 터렛에서 계산한 적과의 거리를 줍니다.
(x1, y1, r1, x2, y2, r2)
문제 풀이 방향을 생각해보면 몇가지 경우의 수가 나옵니다.
우선 문제 풀이 방향은 크기가 같거나 다른 두 개의 원이 접하는 점의 개수를 측정하는 것입니다.
위 생각에 도달했다면 우리는 모든 경우의 수를 생각해볼 수 있습니다.
- 2개의 원이 완전히 겹치는 경우
- 2개의 원이 1개의 접점을 가지는 경우
- 2개의 원이 2개의 접점을 가지는 경우
- 2개의 원이 접하지 않을 경우
위 4개의 경우의 수를 계산할 수 있으면 이 문제를 해결할 수 있습니다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test_case = sc.nextInt();
for (int i = 0; i < test_case; i++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int r1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int r2 = sc.nextInt();
if (x1 == x2 && y1 == y2) {
if(r1==r2){
System.out.println(-1);
}else{
System.out.println(0);
}
} else {
double dis1 = Math.abs(x1 - x2);
dis1 = Math.pow(dis1, 2);
double dis2 = Math.abs(y1 - y2);
dis2 = Math.pow(dis2, 2);
double dis = dis1 + dis2;
dis = Math.sqrt(dis);
if (dis == r1 + r2) {
System.out.println(1);
} else if (dis > r1 + r2) {
System.out.println(0);
} else {
if (Math.abs(r1-r2)>dis) {
System.out.println(0);
} else if (Math.abs(r1-r2)==dis) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
}
}
}