https://school.programmers.co.kr/learn/courses/30/lessons/1844
function solution(maps) {
return (bfs = (startY, startX) => {
const [height, width] = [maps.length, maps[0].length],
[queue, valueVisit] = [[], maps.map(i => i.map(v => 0))];
const directionY = [0, 0, -1, 1],
directionX = [-1, 1, 0, 0];
queue.push([startY, startX]);
valueVisit[startY][startX] = maps[startY][startX];
while (queue.length) {
const [y, x] = queue.shift();
for (let i = 0; i < 4; i++) {
const [nextY, nextX] = [y + directionY[i], x + directionX[i]];
if (nextX < 0 || nextY < 0 || nextX >= width || nextY >= height) continue;
if (!maps[nextY][nextX]) continue;
if (valueVisit[nextY][nextX] && valueVisit[y][x] + maps[nextY][nextX] >= valueVisit[nextY][nextX]) continue;
valueVisit[nextY][nextX] = maps[nextY][nextX] + valueVisit[y][x];
queue.push([nextY, nextX]);
}
}
return valueVisit[height - 1][width - 1] === 0 ? -1 : valueVisit[height - 1][width - 1];
})(0, 0);
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] Lv 2. 롤케이크 자르기 (0) | 2023.02.27 |
---|---|
[Programmers] (2021 KAKAO BLIND RECRUITMENT) Lv 2. 메뉴 리뉴얼 (0) | 2023.02.25 |
[Programmers] (2020 KAKAO BLIND RECRUITMENT) Lv 2. 괄호 변환 (0) | 2023.02.25 |
[Programmers] (탐욕법(Greedy)) Lv 2. 큰 수 만들기 (0) | 2023.02.25 |
[Programmers] (stack) Lv 2. 뒤에 있는 큰 수 찾기 (0) | 2023.02.25 |
댓글