관리 메뉴

Jun Hyuk Kim's Blog

1002 - 터렛 본문

Baekjoon Problems/Python

1002 - 터렛

junhyuk1229 2023. 3. 12. 12:34

먼저 각 터렛에 대한 류재명의 위치는 r의 반지름을 가진 원으로 표현이 가능하다. 주어진 케이스마다 2개의 터렛이 주어져서 2개의 원을 그릴 수 있는데, 2개의 원이 겹치는 부분이 류재명이 있을 수 있는 위치이다. 케이스마다 7개의 경우의 수가 발생할 수 있다.

 

1.    터렛 사이의 위치가 같고 반지름의 길이가 같다 -> -1

2.    터렛 사이의 위치가 같고 반지름의 길이가 다르다 -> 0

3.    두 터렛 사이의 거리가 반지름의 차보다 작다 -> 0

4.    두 터렛 사이의 거리가 반지름의 차와 같다 -> 1

5.    두 터렛 사이의 거리가 반지름의 차보가는 크고 반지음의 합보다는 작다 -> 2

6.    두 터렛 사이의 거리가 반지름의 합과 같다 -> 1

7.    두 터렛 사이의 거리가 반지름의 합보다 크다 -> 0

 

터렛 사이의 위치가 같은 경우는 사이의 거리가 0이라고 생각하면 23을 합할 수 있다. 결과적으로는 -1, 02이 출력되는 경우를 if문으로 프린트하면 된다. 출력 결과가 1인 경우는 ‘else’를 사용하여 출력하면 된다.

 

 

First, each turret can represent Ryu`s potential position as a circle. Each test case takes 2 turrets that can be changed to 2 circles. The intersections between the 2 circles is the potential position of Ryu. For each test case 7 different scenarios can happen.

 

1.    Turret’s positions are the same and the radius are the same -> -1

2.    Turret’s positions are the same and the radius are different -> 0

3.    Distance between two turrets is lower than the difference of the radii -> 0

4.    Distance between two turrets is the same as the difference of the radii -> 1

5.    Distance between two turrets is greater than the difference of the radii and lower than the sum of the radii -> 2

6.    Distance between two turrets is the same as the sum of the radii -> 1

7.    Distance between two turrets is greater than the sum of the radii -> 0

 

If we correct the top 2 scenarios with the turrets having a distance of 0, we can combine scenarios 2 and 3 together. Finally using multiple if statements we can check for cases with output -1, 0, 2 and use ‘else’ to print the cases with 1 as an output.

 

 

Python Code:

import sys
import math


def get_dist_points(first_point: list, second_point: list) -> float:
    # Get difference between x and y
    diff_x = first_point[0] - second_point[0]
    diff_y = first_point[1] - second_point[1]

    # Get squared distance between the two points
    squared_dist = math.pow(diff_x, 2) + math.pow(diff_y, 2)

    # Return distance between the two points as a float
    return math.sqrt(squared_dist)


def intersect_check(dist_points: float, first_rad: int, second_rad: int) -> int:
    # If both circles are at the same position and has the same radius
    if dist_points == 0 and first_rad == second_rad:
        return -1

    # If one circle is inside another circle
    if dist_points < abs(first_rad - second_rad):
        return 0
    # If both circles are not touching each other
    if dist_points > first_rad + second_rad:
        return 0

    # If both circles meet at 2 points
    if abs(first_rad - second_rad) < dist_points < first_rad + second_rad:
        return 2

    # Other cases
    return 1


def main() -> None:
    # Get test case numbers
    test_num = int(sys.stdin.readline().rstrip())

    # Loop for each test case
    for i in range(test_num):
        # Get input data
        first_x, first_y, first_rad, second_x, second_y, second_rad = map(int, sys.stdin.readline().split(sep=' '))

        # Get distance between two points
        dist_points = get_dist_points([first_x, first_y], [second_x, second_y])

        # Get output number from the radii and the distance between two points
        output_num = intersect_check(dist_points, first_rad, second_rad)

        # Print the output number
        print(output_num)

    return


if __name__ == '__main__':
    main()

'Baekjoon Problems > Python' 카테고리의 다른 글

1003 - 피보나치 함수  (0) 2023.03.12