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

本文完,感谢阅读

相关推荐
CODE_RabbitV3 分钟前
【1分钟速通】 HTML快速入门
前端·html
weixin_459793105 分钟前
SSE 模仿 GPT 响应
前端
rookie fish11 分钟前
Electron+Vite+Vue项目中,如何监听Electron的修改实现和Vue一样的热更新?[特殊字符]
前端·vue.js·electron
晴空闲雲12 分钟前
数据结构与算法-树和二叉树-二叉树的存储结构(Binary Tree)
数据结构·算法
她超甜i15 分钟前
前端通过后端给的webrtc的链接,在前端展示,并更新实时状态
前端·javascript·webrtc
歪歪10021 分钟前
Redux和MobX在React Native状态管理中的优缺点对比
前端·javascript·react native·react.js·架构·前端框架
东风西巷25 分钟前
Atlantis Word Processor:全方位的文字处理专家
前端·学习·word·软件需求
今天不要写bug37 分钟前
基于elementUI实现一个可编辑的表格(简洁版)
前端·javascript·elementui
上优40 分钟前
Vue3纯前端同源跨窗口通信移动AGV小车
前端·vue.js·状态模式
h_k1008640 分钟前
Chrome 插件开发入门技术文章大纲
前端·chrome