LeetCode994. 腐烂的橘子

/**
 * 994. 腐烂的橘子
 * 在给定的网格中,每个单元格可以有以下三个值之一:
 *
 * 值 0 代表空单元格;
 * 值 1 代表新鲜橘子;
 * 值 2 代表腐烂的橘子。
 * 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂。
 *
 * 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1。
 *
 *  
 *
 * 示例 1:
 * 输入:[[2,1,1],[1,1,0],[0,1,1]]
 * 输出:4
 * 示例 2:
 *
 * 输入:[[2,1,1],[0,1,1],[1,0,1]]
 * 输出:-1
 * 解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个正向上。
 * 示例 3:
 *
 * 输入:[[0,2]]
 * 输出:0
 * 解释:因为 0 分钟时已经没有新鲜橘子了,所以答案就是 0 。
 *  
 *
 * 提示:
 *
 * 1 <= grid.length <= 10
 * 1 <= grid[0].length <= 10
 * grid[i][j] 仅为 0、1 或 2
 *
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/rotting-oranges
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 */
typedef struct
{
    int rowNum;
    int colNum;
} pos;
class Solution
{
public:
    int orangesRotting(vector<vector<int>>& grid)
    {
        int row = grid.size();
        int col = grid[0].size();
        int orangeNum = 0;
        vector<pos> posVec;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] != 0) {
                    orangeNum++;
                    if (grid[i][j] == 2) {
                        posVec.push_back({ i, j });
                    }
                }
            }
        }
        if (orangeNum == 0) {
            return 0;
        }
        if (posVec.size() == 0) {
            return -1;
        }
        int times = -1;
        int begin = -1;
        int end = 0;
        pos temp;
        while (begin != end) {
            times++;
            begin = end;
            end = posVec.size();
            for (int i = begin; i < end; i++) {
                temp = posVec[i];
                if (temp.rowNum - 1 >= 0 and grid[temp.rowNum - 1][temp.colNum] == 1) {
                    posVec.push_back({ temp.rowNum - 1, temp.colNum });
                    grid[temp.rowNum - 1][temp.colNum] = 2;
                }
                if (temp.rowNum + 1 < row and grid[temp.rowNum + 1][temp.colNum] == 1) {
                    posVec.push_back({ temp.rowNum + 1, temp.colNum });
                    grid[temp.rowNum + 1][temp.colNum] = 2;
                }
                if (temp.colNum - 1 >= 0 and grid[temp.rowNum][temp.colNum - 1] == 1) {
                    posVec.push_back({ temp.rowNum, temp.colNum - 1 });
                    grid[temp.rowNum][temp.colNum - 1] = 2;
                }
                if (temp.colNum + 1 < col and grid[temp.rowNum][temp.colNum + 1] == 1) {
                    posVec.push_back({ temp.rowNum, temp.colNum + 1 });
                    grid[temp.rowNum][temp.colNum + 1] = 2;
                }
            }
        }
        if (posVec.size() != orangeNum) {
            return -1;
        } else {
            return times - 1;
        }
    }
};
class Solution
{
public:
    int orangesRotting(vector<vector<int>>& grid)
    {
        int row = grid.size();
        int column = grid[0].size();
        bool rot;
        int rotting = 2;
        do {
            rot = false;
            rotting++;
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    if (grid[i][j] > 1 and grid[i][j] < rotting) { //腐烂
                        if ((i - 1) >= 0 and grid[i - 1][j] == 1) {
                            grid[i - 1][j] = rotting;
                            rot = true;
                        }
                        if ((i + 1) < row and grid[i + 1][j] == 1) {
                            grid[i + 1][j] = rotting;
                            rot = true;
                        }
                        if ((j - 1) >= 0 and grid[i][j - 1] == 1) {
                            grid[i][j - 1] = rotting;
                            rot = true;
                        }
                        if ((j + 1) < column and grid[i][j + 1] == 1) {
                            grid[i][j + 1] = rotting;
                            rot = true;
                        }
                    }
                }
            }
        } while (rot);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (grid[i][j] == 1) {
                    return -1;
                }
            }
        }
        return rotting - 3;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值