编辑代码

const rl = require("readline").createInterface({ input: process.stdin });
const iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
  const [row, tate] = (await readline()).split(" ").map(Number);
  const matrix = [];
  const FULL_OIL = 100;
  for (let i = 0; i < row; i++) {
    matrix.push((await readline()).split(" ").map(Number));
  }
  const offsets = [
    [0, 1],
    [1, 0],
    [0, -1],
    [-1, 0],
  ];
  const canReach = (oil) => {
    const maxOil = Array.from({ length: row }, () =>
      Array.from({ length: tate }, () => 0)
    );
    const startOil = oil - matrix[0][0];
    if (startOil < 0) return false;
    if (matrix[0][0] == -1) startOil = FULL_OIL;
    maxOil[0][0] = startOil;
    const queue = [[0, 0, startOil]];
    while (queue.length) {
      const [x, y, oil] = queue.shift();
      if (x == row - 1 && y == tate - 1) return true;
      for (const [dx, dy] of offsets) {
        const nx = x + dx,
          ny = y + dy;
        if (
          nx >= 0 &&
          nx < row &&
          ny >= 0 &&
          ny < tate &&
          matrix[nx][ny] !== 0
        ) {
          let nowOil = oil;
          if (matrix[nx][ny] == -1) nowOil = FULL_OIL;
          if (matrix[nx][ny] > 0) nowOil -= matrix[nx][ny];
          if (nowOil < 0) continue;
          if (maxOil[nx][ny] < nowOil) {
            maxOil[nx][ny] = nowOil;
            queue.push([nx, ny, nowOil]);
          }
        }
      }
    }
    return false;
  };
  let left = 0,
    right = FULL_OIL;
  while (left < right) {
    const mid = (left + right) >> 1;
    if (canReach(mid)) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  console.log(left);
})();