Skip to content

Commit d2866d5

Browse files
committed
게임_맵_최단거리 /중급
1 parent 4a84840 commit d2866d5

File tree

1 file changed

+34
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)