
아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀
구현 아이디어는 대충 이정도 흐름,,이다.
set으로 현재좌표 -> 다음좌표로 길을 저장한다.from collections import deque
def solution(dirs):
cx, cy = 0, 0
visited = set()
directions = {'U': [-1, 0], 'D': [1, 0], 'L': [0, -1], 'R': [0, 1]}
for dir in dirs:
dx, dy = directions[dir]
mx, my = cx + dx, cy + dy
if -5 <= mx <= 5 and -5 <= my <= 5:
visited.add(((cx, cy), (mx, my)) if (cx, cy) < (mx, my) else ((mx, my), (cx, cy)))
cx, cy = mx, my
return len(visited)
function solution(dirs) {
let cx = 0, cy = 0;
const visited = new Set();
const directions = {'U': [-1, 0], 'D': [1, 0], 'L': [0, -1], 'R': [0, 1]};
for (let dir of dirs) {
const [dx, dy] = directions[dir];
const mx = cx + dx;
const my = cy + dy;
if (mx >= -5 && mx <= 5 && my >= -5 && my <= 5) {
const path = [[cx, cy], [mx, my]].sort().toString();
visited.add(path);
[cx, cy] = [mx, my];
}
}
return visited.size;
}
