-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
52 lines (43 loc) · 1.23 KB
/
index.ts
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
function shortestPathBinaryMatrix(grid: number[][]): number {
// Corner cases
if (grid[0][0] === 1) return -1;
const n = grid.length;
const queue: number[][][] = [[]];
let counter = 1;
// Prepare matrix of already visited nodes
const visited = new Array(n);
for (let i = 0; i < visited.length; i++) {
visited[i] = new Array(n).fill(0);
}
let level = [[0, 0]];
visited[0][0] = 1;
let nextLevel = [];
while (level.length !== 0) {
while (level.length !== 0) {
const node = level.pop();
const row = node[0];
const col = node[1];
if (row === n - 1 && col === n - 1) return counter;
for (let nextRow = row - 1; nextRow <= row + 1; nextRow++) {
for (let nextCol = col - 1; nextCol <= col + 1; nextCol++) {
if (nextRow < 0 || nextRow > n - 1 || nextCol < 0 || nextCol > n - 1)
continue;
if (visited[nextRow][nextCol] === 1) continue;
if (grid[nextRow][nextCol] === 1) continue;
nextLevel.push([nextRow, nextCol]);
visited[nextRow][nextCol] = 1;
}
}
}
counter++;
level = nextLevel;
}
return -1;
}
console.log(
shortestPathBinaryMatrix([
[0, 0, 0],
[1, 1, 0],
[1, 1, 0],
])
);