문제 바로가기
접근 방법
- 동서남북 지시대로 움직인다.
- 격자를 벗어나는 경우와 X인 경우를 고려한다.
- 까다로운 점은 명령이 2인데 1만큼 떨어진 곳에 X거나 벗어나는 것을 생각해야 한다.
-> 위치 최신화를 통해 해결
코드
def solution(park, routes):
graph = [list(row) for row in park]
for i, start in enumerate(graph):
if 'S' in start:
y = i
x = start.index('S')
for i in range(len(routes)):
d, step = routes[i].split()
ny = y
nx = x
for j in range(int(step)):
if d == 'E' and nx != len(graph[0]) - 1 and graph[ny][nx + 1] != 'X':
nx += 1
if j == int(step) - 1:
x = nx
elif d == 'W' and nx != 0 and graph[ny][nx - 1] != 'X':
nx -= 1
if j == int(step) - 1:
x = nx
elif d == 'N' and ny != 0 and graph[ny - 1][nx] != 'X':
ny -= 1
if j == int(step) - 1:
y = ny
elif d == 'S' and ny != len(graph) - 1 and graph[ny + 1][nx] != 'X':
ny += 1
if j == int(step) - 1:
y = ny
return [y, x]