본문 바로가기
leetcode 풀이/Greedy

지도에서 상하좌우 움직이기

by 튼튼한개발자 2022. 8. 27.
def isOut(x, y):
  if N >= x > 0 and N >= y > 0 :
    # print(False)
    return False
  # print(True)
  return True

def doAction(action) :
  global x, y, nx, ny
  nx = x
  ny = y 
  
  if action == "R" :    
      nx +=1
      
  if action == "U" :    
      ny -=1
      
  if action == "D" :    
      ny +=1

  if isOut(nx, ny):
    # print("out", nx, ny)
    return

  x, y = nx, ny
  
if __name__=="__main__":
  # plans = input().split()
  plans = ['R', 'R', 'R', 'U', 'D', 'D']

  N = 5
  x, y, nx, ny = 1, 1, 1, 1
  
  print(plans)
  
  for action in plans :
    doAction(action.upper())
  
  print(y, x)

상하좌우를 움직이면서 R, R, R, U, D, D를 하는 경우 마지막으로 위치한 좌표가 어디인지를 묻는 문제

'leetcode 풀이 > Greedy' 카테고리의 다른 글

boj-1931. 회의실배정  (0) 2023.01.20

댓글