🔻PS/Codetree
[Codetree] 작은 구슬의 이동 Python
_니지
2024. 7. 29. 12:12
https://www.codetree.ai/missions/5/problems/small-marble-movement/description
코드트리 | 코딩테스트 준비를 위한 알고리즘 정석
국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.
www.codetree.ai
import sys
input = sys.stdin.readline
n, time = map(int, input().split(" "))
x, y, nd = input().rstrip().split(" ")
x = int(x) - 1
y = int(y) - 1
direct = {"U": 0, "R":1, "D": 2, "L":3}
# U <-> D, R <-> L
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
for t in range(time):
ny = y + dx[direct[nd]]
nx = x + dy[direct[nd]]
if not (0 <= nx and nx < n and 0<= ny and ny <n):
if nd == 'L': nd = "R"
elif nd == 'R': nd = "L"
elif nd == 'U': nd = "D"
elif nd == 'D': nd = "U"
else:
x = nx
y = ny
print(x+1, y+1)
728x90
반응형