-
[이코테 구현-시뮬레이션] 상하좌우Coding Test/이것이 코딩 테스트다 2021. 1. 8. 21:31
# 문제
-
여행가 A는 N × N 크기의 정사각형 공간 위에 서 있다. 이 공간은 1 × 1 크기의 정사각형으로 나누어져 있다.
가장 왼쪽 위 좌표는 (1, 1)이며, 가장 오른쪽 아래 좌표는 (N, N)에 해당한다.
여행가 A는 상, 하, 좌, 우 방향으로 이동할 수 있으며, 시작 좌표는 항상 (1, 1)이다. 우리 앞에는 여행가 A가
이동할 계획이 적힌 계획서가 놓여 있다 -
계획서에는 하나의 줄에 띄어쓰기를 기준으로 L, R, U, D 중 하나의 문자가 반복적으로 적혀있다.
각 문자의 의미는 다음과 같다- L: 왼쪽으로 한 칸 이동
- R: 오른쪽으로 한 칸 이동
- U: 위로 한 칸 이동
- D: 아래로 한 칸 이동
-
이때 여행가 A가 N × N 크기의 정사각형 공간을 벗어나는 움직임은 무시된다
예를 들어 (1, 1)의 위치에서 L 혹은 U를 만나면 무시된다
다음은 N = 5인 지도와 계획이다
내 풀이
n = int(input()) pList = input().split() x, y = 1, 1 for i in pList: if i == 'L' and y != 1: y -= 1 if i == 'R' and y != n: y += 1 if i == 'U' and x != 1: x -= 1 if i == 'D' and x != n: x += 1 print(x, y)
지향할 답안
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)
개선할 점
방향 벡터를 잘 활용하는 연습을 해보자.
'Coding Test > 이것이 코딩 테스트다' 카테고리의 다른 글
[이코테 DFS & BFS] 음료수 얼려 먹기 (0) 2021.01.09 [이코테 구현-완전탐색] 시각 (0) 2021.01.08 [이코테 Greedy] 볼링공 고르기 (0) 2021.01.08 [이코테 Greedy] 만들 수 없는 금액 (0) 2021.01.08 [이코테 Greedy] 모험가 길드 (0) 2021.01.08 -