알고리즘
1002 - 터렛
hongs429
2023. 5. 24. 22:21
728x90
https://www.acmicpc.net/problem/1002
1002번: 터렛
각 테스트 케이스마다 류재명이 있을 수 있는 위치의 수를 출력한다. 만약 류재명이 있을 수 있는 위치의 개수가 무한대일 경우에는 -1을 출력한다.
www.acmicpc.net
문제요약
- 레이더의 두 점의 좌표와 좌표로부터 멀어질 수 있는 거리가 주어짐
- 두 레이더가 만나는지 여부를 파악하는 문제
- 두점에서만나면 2, 한 점에서 만나면 1, 만나지 않으면 0, 모든 거리에서 만나면, -1 을 출력
두 원의 접점을 구하는 문제
경우의 수를 나누는 것이 핵심
1 - x : 주 중심의 거리(피타고라스)와 반지름들의 합 과의 비교
2 - x : 두 중심의 거리(|a - b|)와 반지금들의 합과의 비교
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
for (int i = 0; i < rows; 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();
double distance = ((Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))) * 10.0) / 10;
double tot = r1 + r2;
// 좌표가 동일
if ((x1 == x2) && (y1 == y2)) {
// 반지름이 같은 경우,
if (r1 == r2) System.out.println(-1);
else System.out.println(0);
} else {
// 거리 == 반지름합
if (distance == tot) System.out.println(1);
else if (distance > tot) System.out.println(0);
else {
if(Math.abs(r1 - r2) == distance) System.out.println(1);
else if(Math.abs(r1 - r2) < distance) System.out.println(2);
else System.out.println(0);
}
}
}
}
}
느낀점
예전 수학문제 푸는 것과 비슷해서 재밌었다.
하지만 손으로 푸는 것이 아니라 이를 코드로 구현해야 하기 때문에,
그 부분에서 시간이 좀 소요되었다.
중고등학교 수학문제 푼것 같아 재밌었다.