1. 문제 링크
https://www.acmicpc.net/problem/1085
1085번: 직사각형에서 탈출
한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램
www.acmicpc.net
2. 나는 어떻게 생각했는가?
x, y, w, h = map(int, input().split())
right_distance = w - x
left_distance = x
top_distance = h - y
down_distance = y
result_x = 0
result_y = 0
if right_distance >= left_distance:
result_x = left_distance
else:
result_x = right_distance
if top_distance >= down_distance:
result_y = down_distance
else:
result_y = top_distance
if result_x >= result_y:
print(result_y)
else:
print(result_x)
- 무식하게 모든 경우의 수를 다 생각했다.
- min() 메소드가 있다는 생각을 안했다.
3. 개선된 코드
x, y, w, h = map(int, input().split())
width_diff = w - x
height_diff = h - y
print(min(x, y, width_diff, height_diff))
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
백준 4153 파이썬 - 직각삼각형 (0) | 2021.08.17 |
---|---|
백준 3009 파이썬 - 네 번째 점 (0) | 2021.08.17 |
백준 9020 파이썬 - 골드바흐의 추측 (0) | 2021.08.14 |
백준 4948 파이썬 - 베르트랑 공준 (0) | 2021.08.14 |
백준 1929 파이썬 - 소수 구하기 (0) | 2021.08.13 |
댓글