当前位置:网站首页>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
边栏推荐
- 一. js的深拷贝和浅拷贝
- R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
- ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
- JDBC database addition, deletion, query and modification tool class
- After route link navigation, the sub page does not display the navigation style problem
- Three. Based on ply format point cloud voxel model JS upload interface writing
- Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
- How do BIM swindlers cheat? (turn)
- What is the difference between a host and a server?
- Monte Carlo py solves the area problem! (save pupils Series)
猜你喜欢
How to protect ECs from hacker attacks?
[latex] 5 how to quickly write out the latex formula corresponding to the formula
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Redis cache penetration, cache breakdown, cache avalanche
LeetCode动态规划训练营(1~5天)
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
Recognition of high-speed road signs by Matlab using alexnet
RT-1052学习笔记 - GPIO架构分析
Latest investigation and progress of building intelligence based on sati
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
随机推荐
What is the difference between a host and a server?
如何做产品创新?——产品创新方法论探索一
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
RT-1052学习笔记 - GPIO架构分析
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
PCL点云处理之直线与平面的交点计算(五十三)
Redis的安装(CentOS7命令行安装)
Use the rolling division method to find the maximum common divisor of two numbers
Modeling based on catiav6
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Why does ES6 need to introduce map when JS already has object type
PCL点云处理之基于PCA的几何形状特征计算(五十二)
16MySQL之DCL 中 COMMIT和ROllBACK
Numpy mathematical function & logical function
波场DAO新物种下场,USDD如何破局稳定币市场?
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
【栈和队列专题】—— 滑动窗口
Commit and ROLLBACK in DCL of 16mysql
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration