상하좌우 [구현]

Ji·2022년 3월 23일
0
# 1. A는 N x N 의 정사각형 공간
# 2. 가장 왼쪽 위는 (1,1) / 오른쪽 아래는 (N,N)
# 3. 시작 좌표 (1,1)
# 4. 정사각형 크기 지나가면 무시

n=int(input())
step_list=input().split()


location=[1,1]

for step in step_list:
    if step=='R' and location[1]<n:
        location[1]+=1
    elif step=='L' and location[1]>1:
        location[1]-=1
    elif step=='U' and location[0]>1:
        location[0]-=1
    elif step=='D' and location[0]<n:
        location[0]+=1

    
print(location)
  • list=input.split() 하면 자동으로 리스트로 정렬된다.
# N을 입력받기
n = int(input())
x, y = 1, 1
plans = input().split()

# L, R, U, D에 따른 이동 방향
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_types = ['L', 'R', 'U', 'D']

# 이동 계획을 하나씩 확인
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 < 1 or ny < 1 or nx > n or ny > n:
        continue
    # 이동 수행
    x, y = nx, ny
    
print(x, y)
  • 좌표로 푸는 방법을 숙지
profile
공부방

0개의 댓글

관련 채용 정보