반응형
문제 설명
지나다니는 길을 'O', 장애물을 'X'로 나타낸 직사각형 격자 모양의 공원에서 로봇 강아지가 산책을 하려합니다. 산책은 로봇 강아지에 미리 입력된 명령에 따라 진행하며, 명령은 다음과 같은 형식으로 주어집니다.
- ["방향 거리", "방향 거리" … ]
예를 들어 "E 5"는 로봇 강아지가 현재 위치에서 동쪽으로 5칸 이동했다는 의미입니다. 로봇 강아지는 명령을 수행하기 전에 다음 두 가지를 먼저 확인합니다.
- 주어진 방향으로 이동할 때 공원을 벗어나는지 확인합니다.
- 주어진 방향으로 이동 중 장애물을 만나는지 확인합니다.
위 두 가지중 어느 하나라도 해당된다면, 로봇 강아지는 해당 명령을 무시하고 다음 명령을 수행합니다.
공원의 가로 길이가 W, 세로 길이가 H라고 할 때, 공원의 좌측 상단의 좌표는 (0, 0), 우측 하단의 좌표는 (H - 1, W - 1) 입니다.
공원을 나타내는 문자열 배열 park, 로봇 강아지가 수행할 명령이 담긴 문자열 배열 routes가 매개변수로 주어질 때, 로봇 강아지가 모든 명령을 수행 후 놓인 위치를 [세로 방향 좌표, 가로 방향 좌표] 순으로 배열에 담아 return 하도록 solution 함수를 완성해주세요.
제한사항
- 3 ≤ park의 길이 ≤ 50
- 3 ≤ park[i]의 길이 ≤ 50
- park[i]는 다음 문자들로 이루어져 있으며 시작지점은 하나만 주어집니다.
- S : 시작 지점
- O : 이동 가능한 통로
- X : 장애물
- park[i]는 다음 문자들로 이루어져 있으며 시작지점은 하나만 주어집니다.
- park는 직사각형 모양입니다.
- 3 ≤ park[i]의 길이 ≤ 50
- 1 ≤ routes의 길이 ≤ 50
- routes의 각 원소는 로봇 강아지가 수행할 명령어를 나타냅니다.
- 로봇 강아지는 routes의 첫 번째 원소부터 순서대로 명령을 수행합니다.
- routes의 원소는 "op n"과 같은 구조로 이루어져 있으며, op는 이동할 방향, n은 이동할 칸의 수를 의미합니다.
- op는 다음 네 가지중 하나로 이루어져 있습니다.
- N : 북쪽으로 주어진 칸만큼 이동합니다.
- S : 남쪽으로 주어진 칸만큼 이동합니다.
- W : 서쪽으로 주어진 칸만큼 이동합니다.
- E : 동쪽으로 주어진 칸만큼 이동합니다.
- 1 ≤ n ≤ 9
- op는 다음 네 가지중 하나로 이루어져 있습니다.
아이디어
쉬운 그래프 탐색 구현 문제, 시작점 찾은 이후 routes의 방향과 이동칸수 만큼 이동, 경로에 X가 있다면 이동하지 않는다.
코드
시간복잡도 O(N^2)
def solution(park, routes):
answer=[]
#시작점 찾기
for i in range(len(park)):
for j in range(len(park[0])):
if park[i][j]=='S':
sx,sy=i,j
break
op=[[-1,0],[0,1],[0,-1],[1,0]] #NEWS
for route in routes:
news,n=route.split(' ')
n=int(n)
nx,ny=sx,sy
if news=='N': idx=0
elif news=='E': idx=1
elif news=='W':idx=2
else: idx=3
print(nx,ny)
for i in range(n):
dx,dy=op[idx][0],op[idx][1]
if 0<=sx+dx<len(park) and 0<=sy+dy<len(park[0]) and park[sx+dx][sy+dy]!='X':
sx,sy=sx+dx,sy+dy
else:
sx,sy=nx,ny
break
answer.append([sx,sy])
딕셔너리를 활용해 {N:[-1,0], E:[0,1], W:[0,-1], S:[1,0]}으로 작성했다면 코드가 더 간결했을 것으로 생각된다.
반응형
'Problem Solving > 프로그래머스' 카테고리의 다른 글
[프로그래머스] LV. 1. 달리기 경주 - [Python] (0) | 2024.07.12 |
---|---|
[프로그래머스] LV. 1. 추억 점수 - [Python] (0) | 2024.07.12 |
[프로그래머스] LV. 1. 덧칠하기 - [Python] (0) | 2024.07.12 |
[프로그래머스] LV. 1. 덧칠하기 - [Python] (0) | 2024.07.12 |
[프로그래머스] LV. 1. 대충 만든 자판 - [Python] (1) | 2024.07.12 |