좌표이동의 대표적인 문제인 '상하좌우'를 예시로 들어 해당 유형의 해결 아이디어를 정리하고자 한다.
방향벡터가 핵심이다!
move_types = ['L', 'R', 'U', 'D'] dx = [0, 0, -1, 1] dy = [-1, 1, 0, 0]
for plan in plans: for i in range(len(move_types)): if plan == move_types[i]: nx = x + dx[i] ny = y + dy[i] if nx > n or nx < 1 or ny > n or ny < 1: continue x, y = nx, ny