当前位置:网站首页>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.length
n == grid[i].length
1 <= m, n <= 10
grid[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
边栏推荐
- How to do product innovation—— Exploration of product innovation methodology I
- redis 分布式锁
- 上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
- 波场DAO新物种下场,USDD如何破局稳定币市场?
- 2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
- Some basic configurations in interlij idea
- Numpy - creation of data type and array
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
- Sqoop imports tinyint type fields to boolean type
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
猜你喜欢
How can matlab obtain the truncated image in trainingimagelabeler
【PTA】L1-002 打印沙漏
Latest investigation and progress of building intelligence based on sati
go-zero框架数据库方面避坑指南
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
【栈和队列专题】—— 滑动窗口
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Devexpress 14.1 installation record
随机推荐
Unity 模型整体更改材质
2022 - Data Warehouse - [time dimension table] - year, week and holiday
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
論文寫作 19: 會議論文與期刊論文的區別
ABAQUS script email auto notification
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
【PTA】L2-011 玩转二叉树
Es index (document name) fuzzy query method (database name fuzzy query method)
Servlet learning notes
Implementation of mypromise
Recommend an open source free drawing software draw IO exportable vector graph
Three. Based on ply format point cloud voxel model JS upload interface writing
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Mysql database and table building: the difference between utf8 and utf8mb4
Monte Carlo py solves the area problem! (save pupils Series)
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
Es error: request contains unrecognized parameter [ignore_throttled]
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder