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
};

本文完,感谢阅读

相关推荐
feixing_fx几秒前
告别枯燥字体:Web 字体加载策略与 font-display 最佳实践
前端·css·前端框架·交互·css3
Liora_Yvonne21 分钟前
不懂后端,只会 TypeScript,想独立做完整项目?这套全栈底座就是给前端准备的
前端·后端·全栈
祖力5522 分钟前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
前端Hardy23 分钟前
GitHub 爆火!236K+ Star!一套 Skills 让 AI 按工程师方式写代码
前端·后端
名字还没想好☜28 分钟前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
烬羽28 分钟前
AI Coding 全流程实战:从需求到上线,我用 AI 开发了一个 NPM 包
前端·react.js·ai编程
前进的程序员29 分钟前
Codex破局:前端组件秒级生成|提速React/Vue开发,可复用提示词+实战调试经验
前端·vue.js·react.js·codex
地平线开发者29 分钟前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法
前端snow1 小时前
ai agent ---output汇总
前端
4311媒体网1 小时前
帝国CMS网站自定义搭建技巧
服务器·开发语言·前端