Skip to content

Commit f4f9c7d

Browse files
committed
feat: 미로탐색문제
1 parent b85079f commit f4f9c7d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let input = require('fs').readFileSync('dev/stdin').toString().trim().split('\n');
2+
3+
const [n, m] = input.shift().split(" ");
4+
let graph = input.map(arr => arr.split("").map(x => +x));
5+
6+
const BFS = (n, m, arr) => {
7+
const dx = [-1, 0, 1, 0];
8+
const dy = [0, 1, 0, -1];
9+
10+
let queue = [];
11+
queue.push({x: 0, y: 0});
12+
13+
while (queue.length) {
14+
const target = queue.shift();
15+
for (let i = 0; i < 4; i++) {
16+
const nextX = target.x + dx[i];
17+
const nextY = target.y + dy[i];
18+
19+
if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) {
20+
continue;
21+
}
22+
23+
if (arr[nextX][nextY] !== 1) {
24+
continue;
25+
}
26+
27+
arr[nextX][nextY] = arr[target.x][target.y] + 1;
28+
queue.push({x: nextX, y: nextY});
29+
}
30+
}
31+
return arr[n-1][m-1];
32+
}
33+
34+
const answer = BFS(n, m, graph)
35+
console.log(answer)

0 commit comments

Comments
 (0)