当前位置:网站首页>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
边栏推荐
- 【PTA】L1-002 打印沙漏
- What is the difference between a host and a server?
- [graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
- ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
- Sqoop imports tinyint type fields to boolean type
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- Local call feign interface message 404
- PCL点云处理之计算两平面交线(五十一)
- How does onlyoffice solve no route to host
猜你喜欢

16MySQL之DCL 中 COMMIT和ROllBACK

Browser - learning notes

Click an EL checkbox to select all questions

Recognition of high-speed road signs by Matlab using alexnet

网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)

Tensorflow 2 basic operation dictionary

Automatically fill in body temperature and win10 task plan

波场DAO新物种下场,USDD如何破局稳定币市场?

Five minutes to show you what JWT is

考研英语唐叔的语法课笔记
随机推荐
Numpy sort search count set
2022 - Data Warehouse - [time dimension table] - year, week and holiday
Modeling based on catiav6
Analysis of the relationship between generalized Bim and CAD under the current background
Es error: request contains unrecognized parameter [ignore_throttled]
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
Redis installation (centos7 command line installation)
Linux64Bit下安装MySQL5.6-不能修改root密码
Three. Based on ply format point cloud voxel model JS upload interface writing
三十.什么是vm和vc?
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
Experience of mathematical modeling in 18 year research competition
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
star
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
Scripy tutorial - (2) write a simple crawler
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
如何做产品创新?——产品创新方法论探索一
Why does ES6 need to introduce map when JS already has object type