题目:


思路:

每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎...

如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层

但这里会出现初始时便有多个腐烂橘子的情况,其实道理是一样的,将第一层看成多个而不是一个,同样是层次遍历

这里利用双栈来实现层次遍历:

栈A存储第一批腐烂橘子,将第一批腐烂橘子传染的第二批腐烂橘子的位置全部存储至B,后将A清空,随后将B中第二批传染的第三批的位置存储至A,将B清空,直至AB均空

同时记录新鲜橘子的个数num,若最后num>0,说明不能将所有橘子传染,返回-1

代码:

class Solution {
public:
// row: 行, col: 列, res: 结果, num: 新鲜橘子数
int row, col, i, j, res=, num=;
void search(int i, int j, stack<pair<int, int>>& S, vector<vector<int>>& visited,
vector<vector<int>>& grid){
if(i->= && !visited[i-][j] && grid[i-][j]==) {
S.push(make_pair(i-, j));
visited[i-][j]=;
grid[i-][j] = ;
num--;
}
if(j->= && !visited[i][j-] && grid[i][j-]==) {
S.push(make_pair(i, j-));
visited[i][j-]=;
grid[i][j-] = ;
num--;
}
if(i+<row && !visited[i+][j] && grid[i+][j]==) {
S.push(make_pair(i+, j));
visited[i+][j]=;
grid[i+][j] = ;
num--;
}
if(j+<col && !visited[i][j+] && grid[i][j+]==) {
S.push(make_pair(i, j+));
visited[i][j+]=;
grid[i][j+] = ;
num--;
}
}
int orangesRotting(vector<vector<int>>& grid) {
row = grid.size();
col = grid[].size();
vector<vector<int>> visited(row, vector<int>(col, ));
stack<pair<int, int>> A, B;
for(i=; i<row; i++){
for(j=; j<col; j++){
if(grid[i][j]==){
A.push(make_pair(i, j));
visited[i][j]=;
}else if(grid[i][j]==)
num++;  // 记录新鲜橘子的个数
}
}
while(!A.empty() || !B.empty()){
if(A.empty()){
while(!B.empty()){
i=B.top().first;
j=B.top().second;
search(i, j, A, visited, grid);
B.pop();
}
}else{
while(!A.empty()){
i=A.top().first;
j=A.top().second;
search(i, j, B, visited, grid);
A.pop();
}
}
res++;
}
if(num) return -;
return res==?res:res-;
}
};

注:在最后一层时,没有可以传染的新鲜橘子了,但此时某个栈里仍是非空的(最后一层各个位置),所以res会多加一次,在最后减去就好

同时,如果没有橘子或者本身就是腐烂橘子,res值为0,此时不需要减去1

 

[LeetCode] 994. Rotting Oranges 腐烂的橘子的更多相关文章

  1. [leetcode] 994. Rotting Oranges

    题目 You are given an m x n grid where each cell can have one of three values: 0 representing an empty ...

  2. 【Leetcode_easy】994. Rotting Oranges

    problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完

  3. 【LeetCode】994. Rotting Oranges 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetco ...

  4. 【leetcode】994. Rotting Oranges

    题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...

  5. Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)

    Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  6. [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges

    In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...

  7. Leetcode春季打卡第四天:994. 腐烂的橘子

    Leetcode春季打卡第四天:994. 腐烂的橘子 Leetcode春季打卡第四天:994. 腐烂的橘子 思路 思路是采用广度优先搜索,一层一层遍历. 首先先扫描矩阵,将坏橘子放进队列,记录正常橘子 ...

  8. leetcode 994.腐烂的橘子

    题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂 ...

  9. Rotting Oranges - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Rotting Oranges - LeetCode 注意点 解法 解法一:bfs.首先先统计所有新鲜的橘子数目fresh,如果fresh大于0则一直执行 ...

随机推荐

  1. 吴裕雄--天生自然 JAVA开发学习:序列化

    public final void writeObject(Object x) throws IOException public final Object readObject() throws I ...

  2. rest framework-版本-长期维护

    ###############  版本   ############### # # 版本的问题: # rest_framework.versioning.URLPathVersioning # 一般就 ...

  3. bwa index|amb|ann|bwt|pac|sa

    -.gapcloser.fa | > t1.fa bwa index -a bwtsw -p t1 t1.fa >t1.bwa_index.log >& #$ ll #tot ...

  4. Events|sample space|mutually exclusive events

    5.2Events The collection of all 52 cards—the possible outcomes—is called the sample space for this e ...

  5. 电脑莫名重启,VS代码丢失的解决办法

    今天写了一天的代码,然后电脑放在公司了,出去看电影(公司组织红色文化培训..)回来发现电脑重启,再打开电脑,VS的代码都不见了.好慌.... 别慌处理办法来了: 打开everything(没有的可以下 ...

  6. 5-7 学生cpp成绩统计

    完成“学生cpp成绩计算”之后,修改Person和Student类,各自增加两个无参构造函数. 仍以Person类为基础,建立一个派生类Teacher,增加以下成员数据: int ID;//教师工号 ...

  7. idea生成serialVersionUID

    默认情况下Intellij IDEA不会提示继承了Serializable接口的类生成serialVersionUID的警告.如果需要生成serialVersionUID,就要在Preferences ...

  8. 1.redis安装配置

    Redis 1.Redis安装: 说明: 1.也是一种类似于Memcached的kev-value机制的存储服务 2.是非关系型数据库(NoSQL)的一种. 3.官网:www.redis.io,中文网 ...

  9. [LC] 61. Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  10. Pytorch中的variable, tensor与numpy相互转化的方法

    1.将numpy矩阵转换为Tensor张量 sub_ts = torch.from_numpy(sub_img) #sub_img为numpy类型 2.将Tensor张量转化为numpy矩阵 sub_ ...