Baekjoon 14530번 [The Lost Cow]
https://www.acmicpc.net/problem/14530
14530번: The Lost Cow
The single line of input contains two distinct space-separated integers \(x\) and \(y\). Both are in the range \(0 \ldots 1,000\).
www.acmicpc.net
- 사용언어: Python (PyPy3)
- 알고리즘: 수학, 구현, 시뮬레이션
문제
코드
문제의 조건을 잘 확인해야 함
처음엔 이동한 후 위치에서 +1, -2, +4, -8, ... 이런 식으로 이동하는 건줄 알았는데, 알고 보니 처음 John의 위치인 x에서 +1, -2, +4, -8, ... 인 위치로 이동하는 거였음
x, y = map(int, input().split())
cur_John = x # John의 현재 위치
travel_distance, travel_direction = 1, 1 # 이동 거리, 이동 방향
idx = 1
ans = 0
while True:
for _ in range(travel_distance):
cur_John += travel_direction
ans += 1
if cur_John == y:
print(ans)
exit()
idx *= 2 # 이동할 위치는 x로부터 2배씩 멀어짐
travel_distance = abs(cur_John-x) + idx
travel_direction *= (-1)
'백준 > USACO bronze 기출' 카테고리의 다른 글
[백준] 16769번 Mixing Milk (0) | 2022.10.12 |
---|---|
[백준] 15593번 Lifeguards (Bronze) (0) | 2022.10.03 |
[백준] 14175번 The Cow-Signal (0) | 2022.10.03 |
[백준] 15751번 Teleportation (0) | 2022.10.03 |
[백준] 10675번 Cow Routing (0) | 2022.10.03 |