티스토리 뷰

교재에 서술된 정답 코드가 아닌, 제가 직접 작성한 코드입니다.


n = int(input())
steps = list(input().split())

move = { 'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0) }
x, y = 1, 1

for step in steps :
  xd, xy = move[step][0], move[step][1]

  x += xd
  y += xy

  if x == 0 :
    x = 1
  elif x == n + 1 :
    x = n
  elif y == 0 :
    y = 1
  elif y == n + 1 :
    y = n

print(x, y)

 

 

개선된 버전

n = int(input())
steps = list(input().split())

move = { 'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0) }
x, y = 1, 1

for step in steps :
  xd = x + move[step][0]
  yd = y + move[step][1]

  if xd > n or xd < 1 or yd > n or yd < 1 :
    continue

  x, y = xd, yd

print(x, y)
728x90