당구 연습
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
- 풀이
- 왼,오,위,아래 쿠션
def solution(m, n, startX, startY, balls):
answer = []
for ball in balls:
x,y=ball
diffX = startX-x
diffY = startY-y
left = (startX+x)**2 + (diffY**2) # 왼쪽 쿠션
right = ((m-startX)+(m-x))**2 + (diffY**2) # 오른쪽 쿠션
top = (diffX**2) + ((n-startY)+(n-y))**2 # 위쪽 쿠션
bottom = (diffX**2) + (startY+y)**2 # 아래쪽 쿠션
if diffX == 0: # X축 같은 선상일 때
if diffY > 0: # 아래쪽 방향 쿠션 안됨
res = min(left, right, top)
else : # 위쪽 방향 쿠션 안됨
res = min(left, right, bottom)
elif diffY == 0: # Y축 같은 선상일 때
if diffX > 0: # 왼쪽 쿠션 안됨
res = min(right, top, bottom)
else : # 오른쪽 쿠션 안됨
res = min(left, top, bottom)
else: # 같은 축 없을 때
res = min(left, right, top, bottom)
answer.append(res)
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스][DP] 스티커 모으기(2) (0) | 2025.03.05 |
|---|---|
| [프로그래머스][구현] 주차 요금 계산 (0) | 2025.03.03 |
| [프로그래머스][약수,배수][DP] 억억단을 외우자 (0) | 2025.02.20 |
| [프로그래머스][BFS] 지게차와 크레인 (0) | 2025.02.17 |
| [프로그래머스][구현] 문자열 압축 (1) | 2025.02.08 |