입출력 예시
나의 코드
# test case 하나 실패
def get_dist(m, n, a, b, c, d):
'''
a, b: startX, startY
c, d: endX, endY
'''
dist = []
if (a+c) <= m : # left wall
if not (b == d and a > c):
dist.append((a+c)**2 + (b-d)**2)
else: # right wall
if not (b == d and a < c):
dist.append((2*m-a-c)**2 + (b-d)**2)
if (b+d) <= n: # bottom wall
if not (a == c and b > d):
dist.append((b+d)**2 + (c-a)**2)
else: # top wall
if not (a == c and b < d):
dist.append((2*n-b-d)**2 + (c-a)**2)
return min(dist)
def solution(m, n, startX, startY, balls):
answer = []
for ball in balls:
answer.append(get_dist(m, n, startX, startY, ball[0], ball[1]))
return answer
# 모두 성공
def get_dist(m, n, a, b, c, d):
'''
a, b: startX, startY
c, d: endX, endY
'''
dist = []
if not (b == d and a > c): # left wall
dist.append((a+c)**2 + (b-d)**2)
if not (b == d and a < c): # right wall
dist.append((2*m-a-c)**2 + (b-d)**2)
if not (a == c and b > d): # bottom wall
dist.append((b+d)**2 + (c-a)**2)
if not (a == c and b < d): # top wall
dist.append((2*n-b-d)**2 + (c-a)**2)
return min(dist)
def solution(m, n, startX, startY, balls):
answer = []
for ball in balls:
answer.append(get_dist(m, n, startX, startY, ball[0], ball[1]))
return answer
Python
복사
개선점 분석
•
왜 첫 코드는 반례가 있는거고, 반례가 어떤게 있을까?
left/right, top/bottom 중 하나만 해도 된다고 생각해서 첫 코드처럼 썼는데..