30 天刷题挑战(十七)

题目来源: LeetCode 75 30 天 JavaScript 挑战

1926. 迷宫中离入口最近的出口

思路

广度优先算法,初始化第一步位置,每一步可以走上下左右,记录走过的位置,最后能到达迷宫边界就返回对应步数,否则返回 -1。

代码

js 复制代码
/**
 * @param {character[][]} maze
 * @param {number[]} entrance
 * @return {number}
 */
const nearestExit = function(maze, entrance) {
  const walkableArr = [[...entrance, 0]]
  const pos = [
    {x: -1, y: 0},
    {x: 1, y: 0},
    {x: 0, y: -1},
    {x: 0, y: 1},
  ]

   while(walkableArr.length) {
     const [row, col, step] = walkableArr.shift()
     
     // 尝试上下左右行走
     for(let i = 0; i < pos.length; i++) {
       const {x, y} = pos[i]
       const nextX = row + x
       const nextY = col + y
       const nextRow = maze[nextX]

       if (nextRow) {
         if (nextRow[nextY] === '.') {
            // 到达边迷宫界,找到出口
            if (nextX === 0 || nextX === maze.length - 1 || nextY === 0 || nextY === maze[nextX].length - 1) {
              return step + 1
            }
            // 记录走过的地方
            maze[nextX][nextY] = '+'
            // 往下一个格子走
            walkableArr.push([nextX, nextY, step + 1])
         }
       }
     }
     // 记录走过的地方
     maze[row][col] = '+'
   }

   return -1
};

994. 腐烂的橘子

思路

广度优先遍历,统计好橘子的个数,使用一个队列保存坏橘子的坐标;遍历坏橘子队列,将坏橘子逐一出列,在它的 4 个方向腐化好橘子,最后返回使用时间或者 -1 表示无法完全腐化;

代码

js 复制代码
/**
 * @param {number[][]} grid
 * @return {number}
 */
const orangesRotting = function(grid) {
  let unrotten = 0
  const queue = []
  const height = grid.length
  const width = grid[0].length
 
 // 遍历网格
  for (let i = 0; i < height; i++) {
    for(let j = 0; j < width; j++) {
      if (grid[i][j] === 2) {
        // 所有的坏橘子的坐标推入队列
        queue.push([i, j])
      } else if (grid[i][j] === 1) {
        // 统计好橘子的个数
        unrotten++
      }
    }
  }

  if (unrotten === 0) {
    return 0
  }

  // 腐化用时
  let level = 0
  // 4 个方向
  const pos = [
    {x: -1, y: 0},
    {x: 1, y: 0},
    {x: 0, y: -1},
    {x: 0, y: 1},
  ]

  // 遍历坏橘子队列
  while(queue.length) {
    // 时间加一
    level++

    let levelSize = queue.length

    for(let i = 0; i < levelSize; i++) {
     // 坏橘子出列
      let current = queue.shift()
      
      // 尝试 4 个方向
      for(let j = 0; j < pos.length; j++) {
        let p = pos[j]
        let x = current[0] + p.x
        let y = current[1] + p.y

        // 跳过超出边界或本身就不好的橘子
        if (x < 0 || x >= height || y < 0 || y >= width || grid[x][y] !== 1 ) {
          continue
        }
        // 腐化好橘子
        grid[x][y] = 2
        queue.push([x, y])
        unrotten--
      }
    }
  }

  // 返回用时或者 -1
  return unrotten === 0 ? level - 1 : -1
};

本文完,感谢阅读

相关推荐
PXM的算法星球2 小时前
【leetcode】3524 求出数组的X值1
算法·leetcode·职场和发展
萌萌哒草头将军4 小时前
⚡⚡⚡尤雨溪宣布开发 Vite Devtools,这两个很哇塞 🚀 Vite 的插件,你一定要知道!
前端·vue.js·vite
_一条咸鱼_5 小时前
揭秘 Android TextInputLayout:从源码深度剖析其使用原理
android·java·面试
_一条咸鱼_5 小时前
揭秘!Android VideoView 使用原理大起底
android·java·面试
椰羊~王小美5 小时前
LeetCode -- Flora -- edit 2025-04-27
算法·leetcode·职场和发展
_一条咸鱼_5 小时前
深度揭秘!Android TextView 使用原理全解析
android·java·面试
小彭努力中5 小时前
7.Three.js 中 CubeCamera详解与实战示例
开发语言·前端·javascript·vue.js·ecmascript
_一条咸鱼_5 小时前
深度剖析:Android Canvas 使用原理全揭秘
android·java·面试