[알고리즘] 백준 하키 - S4

eternal moment·2024년 10월 12일
0

https://www.acmicpc.net/problem/1358

2024.10.12 풀이

import sys
input = sys.stdin.readline

w,h,x,y,p = map(int, input().split())
r=h//2
x1,y1=x,y+r
x2,y2=x+w,y+r

cnt=0
for _ in range(p):
    a,b=map(int, input().split())
    if a<x1:
        if (x1-a)**2+(y1-b)**2<=r**2:
            cnt+=1
            # print(a,b)
    elif a>x2:
        if (x2-a)**2+(y2-b)**2<=r**2:
            cnt+=1
            # print(a,b)
    else:
        if y<=b<=y+h:
            cnt+=1
            # print(a,b)

print(cnt)

다른 풀이

import math
w, h, x, y, p = map(int, input().split())

# 반지름, 각 원의 중심 좌표
r = h / 2
c1x, c1y = x, y+r
c2x, c2y = x+w, y+r

count = 0 # 링크 안에 있는 사람 수 

for _ in range(p):
  px, py = map(int, input().split())

  # 각 원의 중심에서의 거리
  d1 = math.sqrt((c1x - px) ** 2 + (c1y - py) ** 2)
  d2 = math.sqrt((c2x - px) ** 2 + (c2y - py) ** 2)

  if d1 <= r or d2 <= r:
    count += 1
  elif x <= px <= x+w and y <= py <= y+h:
    count += 1

print(count)
import sys
input = sys.stdin.readline

W, H, X, Y, P = map(int, input().split())
count = 0
for _ in range(P):
    x, y = map(int, input().split())
    
    # 가운데 직사각형 내부에 있는 경우(둘레 포함)
    if (X <= x <= X+W) and (Y <= y <= Y+H):
        count +=1
        continue
    
    # 왼쪽 반원, 오른쪽 반원 내부에 있는 경우(둘레 포함)
    R = H/2
    d1 = ((x-X)**2 + (y-(Y+R))**2)**0.5
    d2 = ((x-(X+W))**2 + (y-(Y+R))**2)**0.5
    if d1 <= R or d2 <= R:
        count += 1

print(count)

check point

  • 원의 내부에 있는지 아닌지 여부를 판별하기 위해서는 원의 방정식 이용.
    - (x-x1)**2 + (y-y1)**2 ==r**2
    - r과 같으면 원의 둘레, 작으면 원 안, 크면 원 밖.

0개의 댓글