多源bfs,LeetCode 994. 腐烂的橘子

一、题目

1、题目描述

在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:

  • 0 代表空单元格;
  • 1 代表新鲜橘子;
  • 2 代表腐烂的橘子。

每分钟,腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。

返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1

2、接口描述

python3
复制代码
python 复制代码
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
cpp
复制代码
cpp 复制代码
class Solution {
public:
    int orangesRotting(vector<vector<int>>& grid) {

    }
};

3、原题链接

994. 腐烂的橘子


二、解题报告

1、思路分析

初始遍历网格将所有腐烂橘子入队,同记录当前所有的新鲜橘子个数

如果队列不空并且有新鲜橘子,我们就将当前队列的坐标全部出队扩展一层,只能扩展那些新鲜橘子的格子,并将其改为不新鲜

最终返回时如果仍有新鲜橘子就返回-1,否则返回bfs扩展的层数即时间

2、复杂度

时间复杂度: O(NM) 空间复杂度:O(NM)

3、代码详解

python3
复制代码
python 复制代码
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        q = []
        tot = 0
        for i, row in enumerate(grid):
            for j, x in enumerate(row):
                if x == 1:
                    tot += 1
                elif x == 2:
                    q.append((i, j))
        ret = 0
        while q and tot:
            ret += 1
            nq = []
            for x, y in q:
                for nx, ny in (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1):
                    if nx < 0 or ny < 0 or nx >= m or ny >= n or grid[nx][ny] != 1:
                        continue
                    grid[nx][ny] = 2
                    tot -= 1
                    nq.append((nx, ny))
                q = nq

        return ret if tot == 0 else -1
cpp
复制代码
cpp 复制代码
class Solution {
public:
typedef pair<int, int> PII;
static constexpr int dst[5] { 1, 0, -1, 0, 1 };
    int orangesRotting(vector<vector<int>>& grid) {
        queue<PII> q;
        int m = grid.size(), n = grid[0].size();
        int ret = 0, tot = 0;
        for(int i = 0; i < m; i ++)
            for(int j = 0; j < n; j ++)
                if (grid[i][j] == 2) q.emplace(i, j);
                else if(grid[i][j] == 1) tot ++;
        while (q.size() && tot) {
            ret ++;
            for(int i = 0, ed = q.size(); i < ed; i ++) {
                auto [x, y] = q.front();
                q.pop();
                for (int j = 0; j < 4; j ++) {
                    int nx = x + dst[j], ny = y + dst[j + 1];
                    if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
                    if (grid[nx][ny] != 1) continue;
                    tot -= grid[nx][ny];
                    grid[nx][ny] = 2;
                    q.emplace(nx, ny);
                }
            }
        }
        return tot ? -1 : ret;
    }
};
相关推荐
居然有人6544 分钟前
23贪心算法
数据结构·算法·贪心算法
SylviaW081 小时前
python-leetcode 37.翻转二叉树
算法·leetcode·职场和发展
h^hh1 小时前
洛谷 P3405 [USACO16DEC] Cities and States S(详解)c++
开发语言·数据结构·c++·算法·哈希算法
玦尘、1 小时前
位运算实用技巧与LeetCode实战
算法·leetcode·位操作
重生之我要成为代码大佬1 小时前
Python天梯赛10分题-念数字、求整数段和、比较大小、计算阶乘和
开发语言·数据结构·python·算法
Best_Me072 小时前
【CVPR2024-工业异常检测】PromptAD:与只有正常样本的少样本异常检测的学习提示
人工智能·学习·算法·计算机视觉
HBryce242 小时前
缓存-算法
算法·缓存
eso19832 小时前
Spark MLlib使用流程简介
python·算法·spark-ml·推荐算法
夏末秋也凉2 小时前
力扣-回溯-93 复原IP地址
算法·leetcode
Erik_LinX2 小时前
算法日记27:完全背包(DFS->记忆化搜索->倒叙DP->顺序DP->空间优化)
算法·深度优先