// 建立辅助数组 const ast = newArray(m + 2); for (let i = 0; i < m + 2; i++) { if (i === 0) ast[i] = newArray(n + 2).fill(1); if (i > 0 && i < m + 1) { ast[i] = readline().split(" ").map(Number); ast[i].push(1); ast[i].unshift(1); } if (i === m + 1) ast[i] = newArray(n + 2).fill(1); }
functionfindPath(ast, i, j) { // 到达终点,直接返回 if (i === m && j === n) return [[i - 1, j -1]];
// 对于走过的点 置为 1 ast[i][j] = 1; if (ast[i - 1][j] === 0) { const up = findPath(ast, i - 1, j); if(up.length) return [[i - 1, j - 1],...up] } if (ast[i + 1][j] === 0) { const down = findPath(ast, i + 1, j); if(down.length) return [[i - 1, j - 1],...down] } if (ast[i][j - 1] === 0) { const left = findPath(ast, i, j - 1); if(left.length) return [[i - 1, j - 1],...left] } if (ast[i][j + 1] === 0) { const right = findPath(ast, i, j + 1); if(right.length) return [[i - 1, j - 1],...right] } return []; }