-
Notifications
You must be signed in to change notification settings - Fork 1
/
게임_맵_최단거리.js
52 lines (43 loc) · 1.07 KB
/
게임_맵_최단거리.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 링크
/*
* 강철원
*/
/*
* 신현호
*/
const dx = [1, -1, 0, 0];
const dy = [0, 0, 1, -1];
const start = {x: 0, y: 0};
const arrive = [];
function solution(maps) {
bfs(maps);
if (arrive.length)
return Math.min(...arrive);
else
return -1;
}
function bfs(maps) {
const queue = [];
const visited = [];
for (let i = 0; i < maps.length; i++)
visited.push(new Array(maps[i].length).fill(0));
queue.push({ x: start.x, y: start.y, depth: 1 });
visited[start.y][start.x] = 1;
while (queue.length) {
const node = queue.shift();
if (node.x === maps[0].length - 1 && node.y === maps.length - 1)
arrive.push(node.depth);
for (let i = 0; i < 4; i++) {
const pos_x = node.x + dx[i];
const pos_y = node.y + dy[i];
if (pos_x < 0 || pos_x >= maps[0].length)
continue;
if (pos_y < 0 || pos_y >= maps.length)
continue;
if (maps[pos_y][pos_x] === 1 && visited[pos_y][pos_x] === 0) {
queue.push({x: pos_x, y: pos_y, depth: node.depth + 1});
visited[pos_y][pos_x] = 1;
}
}
}
}