백준1002. 터렛

김태훈·2024년 1월 10일
0

알고리즘

목록 보기
2/7

백준 1002. 터렛

사실 문제에 대한 이해를 하기 매우 힘들었고, 아직도 조금 어려운 문제입니다.
어째서 마린이 있을 수 있는 위치가 원이 접할 때인가..??

문제에 대한 이해를 하기 힘들어서 다른 분의 글을 보고 이해할 수 있었습니다.

문제는 터렛1, 2의 위치 x, y 와 거리 r을 입력받아 각 터렛이 중점 x, y에 위치할 때 반지름이 r인 원을 그리고 각 터렛이 그리는 원이 겹치는 곳이 몇 개인지 판단하는 문제입니다.

package com.sparta;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();    //테스트 케이스

		while (T-- > 0) {
			int x1 = sc.nextInt();
			int y1 = sc.nextInt();
			int r1 = sc.nextInt();

			int x2 = sc.nextInt();
			int y2 = sc.nextInt();
			int r2 = sc.nextInt();

			// 원의 중점 사이의 거리
			int distance = (int)(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

			// 무한일 때 : 중심이 같고 반지름의 길이도 같을 때
			if (x1 == x2 && y1 == y2 && r1 == r2) {
				System.out.println(-1);
			}
			// 원이 접하지 않을 때(중점 간의 거리가 반지름의 합보다 큼)
			else if (distance > Math.pow(r1 + r2, 2)) {
				System.out.println(0);
			}
            // 원이 접하지 않을 때(원 안에 다른 원이 존재)
            else if(distance < Math.pow(r2 - r1, 2)){
                System.out.println(0);
			}
            // 내접할 때 
            else if (distance == Math.pow(r2 - r1, 2)) {
                System.out.println(1);
            }
            // 외접할 때
            else if (distance == Math.pow(r2 + r1, 2)) {
                System.out.println(1);
            } else {
                System.out.println(2);
            }
        }
	}
}

0개의 댓글

관련 채용 정보