2
* @param {number[][]} grid5
var shortestPathBinaryMatrix = function (grid) {19
const predecessor = [];22
for (let i = 0; i < n; i++) {23
visited.push(Array.from({ length: n }, (v, i) => false));24
distance.push(Array.from({ length: n }, (v, i) => 9999));25
predecessor.push(Array.from({ length: n }, (v, i) => null));28
const startIndex = [0, 0];29
if (grid[startIndex[0]][startIndex[1]] !== 0) {33
queue.push(startIndex);34
distance[startIndex[0]][startIndex[1]] = 1;36
while (queue.length > 0) {37
const current = queue.shift();38
visited[current[0]][current[1]] = true;40
if (current[0] === n - 1 && current[1] === n - 1) {44
directions.forEach((dir) => {45
const x = current[0] + dir[0];46
const y = current[1] + dir[1];48
if (x < 0 || y < 0 || x >= n || y >= n) {54
visited[x][y] === false &&55
distance[x][y] > distance[current[0]][current[1]] + 157
distance[x][y] = distance[current[0]][current[1]] + 1;58
predecessor[x][y] = current;64
console.log(distance);66
console.log(predecessor);68
return distance[n - 1][n - 1] >= 999 ? -1 : distance[n - 1][n - 1];