https://programmers.co.kr/learn/courses/30/lessons/49994
✅ Solution
- 이동했을 때 좌표가 범위 내에 있을 때,
- visited에 있는지 없는지 판단한다.
- 없다면 visited에 추가해주고, answer += 1을 해준다.
✅ Code
def solution(dirs):
answer = 0
visited = []
x, y = 5, 5
move = {'U': (-1, 0), 'D': (1, 0), 'R': (0, 1), 'L': (0, -1)}
for dir in dirs:
nx, ny = x + move[dir][0], y + move[dir][1]
if 0 <= nx < 11 and 0 <= ny < 11:
if not ([[x, y], [nx, ny]] in visited or [[nx, ny], [x, y]] in visited):
answer += 1
visited.append([[x, y], [nx, ny]])
x, y = nx, ny
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 땅따먹기 (Python) (0) | 2022.06.28 |
---|---|
[프로그래머스] Level 2 - 스킬 트리 (Python) (0) | 2022.06.27 |
[프로그래머스] Level 2 - 가장 큰 정사각형 찾기 (Python) (0) | 2022.06.27 |
[프로그래머스] Level 2 - 쿼드압축 후 개수 세기 (Python) (0) | 2022.06.25 |
[프로그래머스] Level 2 - n^2 배열 자르기 (Python) (0) | 2022.06.25 |