코테42) 직사각형에서 탈출

gyu·2024년 5월 8일

Algorithm

목록 보기
43/45

📝 문제설명

한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램을 작성하시오.

입력
첫째 줄에 x, y, w, h가 주어진다.

출력
첫째 줄에 문제의 정답을 출력한다.

제한
1 ≤ w, h ≤ 1,000
1 ≤ x ≤ w-1
1 ≤ y ≤ h-1
x, y, w, h는 정수

내 풀이:

#assign 4 inputs to each variable
x,y,w,h = map(int, input().split())
#calculates the distances from the point (x, y) to the four sides of the rectangle
#store them into list
distance=[x,y, h-y, w-x]
#find min value of the list(shortest distance)
print(min(distance))

처음엔 순간 (x,y)에서 (w,h)까지 가는 최솟값을 구하라는 건가 했는데 경계선이었다.

profile
#TechExplorer 🚀 Curious coder exploring the tech world, documenting my programming journey in a learning journal

1개의 댓글

comment-user-thumbnail
2024년 5월 12일

안녕하세요, 99클럽 그룹 리더 sunni입니다!
꾸준히 TIL을 업로드해주셨군요😆
이번 99클럽 1기는 끝났지만,
앞으로도 힘내서 매일 TIL 도전해보세요! 화이팅입니다 :)
99클럽 https://bit.ly/3TN5TBL

답글 달기