编辑代码

// const { xcode } = require("react-syntax-highlighter/dist/esm/styles/hljs");

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
  const [len, tate] = (await readline()).split(" ").map(Number);
  let arr = (await readline()).split(" ").map(Number);
  const matrix = [];
  const [i, j] = (await readline()).split(" ").map(Number);
  // 将arr转换为矩阵
  while (arr.length > 0) {
    const slice = arr.splice(0, tate);
    matrix.push(slice);
  }
  // console.log(matrix);
  // const start = matrix.filter(([x, y]) => matrix[x][y] > 0);
  const start = [];
  // 遍历矩阵,将大于0的坐标加入start
  for (let i = 0; i < len; i++) {
    for (let j = 0; j < tate; j++) {
      if (matrix[i][j] > 0) start.push([i, j]);
    }
  }
  // console.log(start);
  // 定义偏移量
  const offset = [
    [-1, 0],
    [1, 0],
    [0, -1],
    [0, 1],
  ];
  let ans = 0;
  // const queue = [];
  // 广度优先搜索
  const bfs = (x, y) => {
    const queue = [];
    queue.push([x, y]);
    while (queue.length > 0) {
      const [x, y] = queue.shift();
      for (const [offsetX, offsetY] of offset) {
        const newX = x + offsetX;
        const newY = y + offsetY;
        // if(newX<0||newX>=len||newY<0||newY>=tate) continue;
        if (
          newX >= 0 &&
          newX < len &&
          newY >= 0 &&
          newY < tate &&
          matrix[newX][newY] < matrix[x][y] - 1
        ) {
          matrix[newX][newY] = matrix[x][y] - 1;
          if (matrix[newX][newY] + 0) queue.push([newX, newY]);
        }
      }
    }
  };
  // 遍历start,进行广度优先搜索
  for (let [x, y] of start) {
    bfs(x, y);
    ans = Math.max(ans, matrix[i][j]);
  }
  // 输出结果
  console.log(ans, matrix);
})();