We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f50e86d commit b887f16Copy full SHA for b887f16
1091-shortest-path-in-binary-matrix.js
@@ -0,0 +1,34 @@
1
+/**
2
+ * @param {number[][]} grid
3
+ * @return {number}
4
+ */
5
+const shortestPathBinaryMatrix = function(grid) {
6
+ let N = grid.length,
7
+ qu = [[0, 0]],
8
+ path = 1
9
+ const dirs = [
10
+ [-1, 0],
11
+ [1, 0],
12
+ [0, -1],
13
+ [0, 1],
14
+ [-1, -1],
15
+ [-1, 1],
16
+ [1, -1],
17
+ [1, 1]
18
+ ]
19
+ while (qu.length !== 0) {
20
+ let cop = qu
21
+ qu = []
22
+ for (let [r, c] of cop) {
23
+ if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) continue
24
+ if (grid[r][c] === 1) continue
25
+ grid[r][c] = 1
26
+ if (r === N - 1 && c === N - 1) return path
27
+ for (let dir of dirs) {
28
+ qu.push([r + dir[0], c + dir[1]])
29
+ }
30
31
+ path++
32
33
+ return -1
34
+}
0 commit comments