当前位置:网站首页>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
边栏推荐
- Numpy sort search count set
- Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
- Three. Based on ply format point cloud voxel model JS upload interface writing
- 2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
- Some basic configurations in interlij idea
- Change the material of unity model as a whole
- R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
- JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
- Plato Farm元宇宙IEO上线四大,链上交易颇高
- SQL gets the latest record of the data table
猜你喜欢
Commit and rollback in DCL of 16 MySQL
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
Plato Farm元宇宙IEO上线四大,链上交易颇高
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
[target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Numpy mathematical function & logical function
Click an EL checkbox to select all questions
The ODB model calculates the data and outputs it to excel
随机推荐
The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
Scripy tutorial - (2) write a simple crawler
Redis distributed lock
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
Remote code execution in Win 11 using wpad / PAC and JScript 3
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Monte Carlo py solves the area problem! (save pupils Series)
[latex] 5 how to quickly write out the latex formula corresponding to the formula
NC basic usage 4
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
Redis的安装(CentOS7命令行安装)
Intersection calculation of straight line and plane in PCL point cloud processing (53)
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
NC basic usage
Scrapy教程 - (2)寫一個簡單爬蟲
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
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
DTMF dual tone multi frequency signal simulation demonstration system