[백준/Python] 1004번 - 어린 왕자

Sujin Lee·2022년 7월 7일
0

코딩테스트

목록 보기
83/172
post-thumbnail

문제

백준 1004번 - 어린 왕자

해결 과정

  • 진입/이탈 횟수는 출발점이나 도착점이 행성 내부에 존재할 때 = 두 점 중 하나의 점과 행성 중심과의 거리가 반지름보다 작을 경우
  • 출발점과 도착점 중 하나의 점이 행성 안에 속할 때 진입/이탈 횟수를 증가시킨다

시행착오

  • 경계선에 닿는 조건이 뭘까? 행성 안에 점이 존재하면 경계선에 닿는다
  • 수학적이지 못한가? 예시는 다 맞는데 틀림 반례가 있겠지
import sys

t = int(sys.stdin.readline())
for _ in range(t):
  cnt = 0
  x1, y1, x2, y2 = map(int,sys.stdin.readline().split())
  n = int(sys.stdin.readline())
  for i in range(n):
    cx, cy, r = map(int,sys.stdin.readline().split())
    if cx + (-r) < x1 < cx + r and  cy + (-r) < y1 < cy + r:
      cnt += 1
    if cx + (-r) < x2 < cx + r and  cy + (-r) < y2 < cy + r:
      cnt += 1
  print(cnt)

풀이

import sys

t = int(sys.stdin.readline())
for _ in range(t):
  cnt = 0
  x1, y1, x2, y2 = map(int,sys.stdin.readline().split())
  n = int(sys.stdin.readline())
  for i in range(n):
    cx, cy, r = map(int,sys.stdin.readline().split())
    # 두 점 사이의 거리
    dist1 = ((cx - x1) **2 + (cy - y1) **2) ** 0.5
    dist2 = ((cx - x2) **2 + (cy - y2) **2) ** 0.5
    if dist1 > r and dist2 < r:
      cnt += 1
    if dist2 > r and dist1 < r:
      cnt += 1
  print(cnt)
profile
공부한 내용을 기록하는 공간입니다. 📝

0개의 댓글