当前位置:网站首页>LeetCode 994、腐烂的橘子
LeetCode 994、腐烂的橘子
2022-04-23 20:23:00 【亡于灬】
994、腐烂的橘子
1)题目描述
在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一:
- 值
0代表空单元格; - 值
1代表新鲜橘子; - 值
2代表腐烂的橘子。
每分钟,腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。
返回 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1 。
示例 1:

输入:grid = [[2,1,1],[1,1,0],[0,1,1]]
输出:4
示例 2:
输入:grid = [[2,1,1],[0,1,1],[1,0,1]]
输出:-1
解释:左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个正向上。
示例 3:
输入:grid = [[0,2]]
输出:0
解释:因为 0 分钟时已经没有新鲜橘子了,所以答案就是 0 。
提示:
m == grid.lengthn == grid[i].length1 <= m, n <= 10grid[i][j]仅为0、1或2
2)分析
广度优先搜索。
- 遍历
grid存储所有烂橘子的位置,入队,并统计新鲜橘子的数量; - 然后遍历队列,如果四周存在新鲜橘子 ,那么设置时间,然后添加进队列,新鲜橘子数量减一;
- 广度优先搜索结束后,如果新鲜橘子数量不为零,返回
-1。
3)C++代码
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int dx[4]={
1,0,-1,0};
int dy[4]={
0,1,0,-1};
int cnt_1=0;
int res=0;
queue<pair<int,int>> que;
vector<vector<int>> timeCosted(m,vector<int> (n,0));
//遍历网格,记录所有腐烂的橘子的坐标与新鲜橘子的数量
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==2)
que.emplace(i,j);
else if(grid[i][j]==1)
cnt_1++;
}
}
//广度优先搜索
while(!que.empty()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<m&&newY>=0&&newY<n&&grid[newX][newY]==1&&!timeCosted[newX][newY]){
timeCosted[newX][newY]=timeCosted[x][y]+1;
que.emplace(newX,newY);
if(grid[newX][newY]==1){
res=res>timeCosted[newX][newY]?res:timeCosted[newX][newY];
cnt_1-=1;
if(!cnt_1)
break;
}
}
}
}
return cnt_1?-1:res;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360454
边栏推荐
- Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
- 网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
- Es error: request contains unrecognized parameter [ignore_throttled]
- DTMF双音多频信号仿真演示系统
- SQL gets the latest record of the data table
- 論文寫作 19: 會議論文與期刊論文的區別
- How can matlab obtain the truncated image in trainingimagelabeler
- Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
- Scripy tutorial - (2) write a simple crawler
- Investigate why close is required after sqlsession is used in mybatties
猜你喜欢

【栈和队列专题】—— 滑动窗口

【PTA】整除光棍

Five minutes to show you what JWT is

16MySQL之DCL 中 COMMIT和ROllBACK
![[latex] 5 how to quickly write out the latex formula corresponding to the formula](/img/1f/3c5a332ce1d6852dde38040faea5bf.png)
[latex] 5 how to quickly write out the latex formula corresponding to the formula

SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明

Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
![Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]](/img/1a/669c330e64af8e75f4b05e472d03d3.png)
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]

考研英语唐叔的语法课笔记

. Ren -- the intimate artifact in the field of vertical Recruitment!
随机推荐
NC basic usage 3
Commit and ROLLBACK in DCL of 16mysql
2022 - Data Warehouse - [time dimension table] - year, week and holiday
Mysql database backup scheme
如何做产品创新?——产品创新方法论探索一
R语言ggplot2可视化:ggplot2可视化散点图并使用geom_mark_ellipse函数在数据簇或数据分组的数据点周围添加椭圆进行注释
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Why does ES6 need to introduce map when JS already has object type
DTMF双音多频信号仿真演示系统
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
How about CICC fortune? Is it safe to open an account
Redis distributed lock
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
NC basic usage 1
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
Investigate why close is required after sqlsession is used in mybatties
go-zero框架数据库方面避坑指南
Remote code execution in Win 11 using wpad / PAC and JScript 1
Remote code execution in Win 11 using wpad / PAC and JScript 3
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]