Muscardinus
게임 맵 최단거리 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/1844
function solution(maps) {
let answer;
const dy = [1, -1, 0, 0];
const dx = [0, 0, 1, -1];
const n = maps.length;
const m = maps[maps.length - 1].length;
const check = Array.from(new Array(n), () => new Array(m).fill(0));
const q = [];
let h = 0;
let t = 1;
q.push([0, 0]);
check[0][0] = 1;
while (h !== t) {
const [y, x] = q[h++];
for (let i = 0; i < 4; i++) {
const ny = y + dy[i];
const nx = x + dx[i];
if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
if (check[ny][nx]) continue;
if (maps[ny][nx] === 0) continue;
q[t++] = [ny, nx];
check[ny][nx] = check[y][x] + 1;
}
}
return check[n - 1][m - 1] ? check[n - 1][m - 1] : -1;
}
728x90
Comments