Skip to content

Commit da4b57f

Browse files
committed
게임 맵 최단거리 / 중급
1 parent a3021f1 commit da4b57f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function solution(maps) {
2+
const n = maps.length;
3+
const m = maps[0].length;
4+
5+
const visited = Array.from(Array(n), () => Array(m).fill(false));
6+
7+
const dx = [-1, 1, 0, 0];
8+
const dy = [0, 0, -1, 1];
9+
10+
const queue = [[0, 0, 1]];
11+
visited[0][0] = true;
12+
13+
while (queue.length > 0) {
14+
const [x, y, count] = queue.shift();
15+
16+
if (x === n - 1 && y === m - 1) {
17+
return count;
18+
}
19+
20+
for (let i = 0; i < 4; i++) {
21+
const nx = x + dx[i];
22+
const ny = y + dy[i];
23+
24+
if (
25+
nx >= 0 && nx < n &&
26+
ny >= 0 && ny < m &&
27+
maps[nx][ny] === 1 &&
28+
!visited[nx][ny]
29+
) {
30+
visited[nx][ny] = true;
31+
queue.push([nx, ny, count + 1]);
32+
}
33+
}
34+
}
35+
36+
return -1;
37+
}

0 commit comments

Comments
 (0)