当前位置:网站首页>994. 腐烂的橘子-非递归法
994. 腐烂的橘子-非递归法
2022-06-23 05:48:00 【Mr Gao】
994. 腐烂的橘子
在给定的 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 。
解题代码如下:
int orangesRotting(int** grid, int gridSize, int* gridColSize){
int n=gridSize;
int m=gridColSize[0];
int fresh_man[m*n][2];
int num=0;
int i,j;
int size=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(grid[i][j]==1){
fresh_man[size][0]=i;
fresh_man[size][1]=j;
size++;
}
}
}
int drection[4][2]={
{
0,1},{
1,0},{
0,-1},{
-1,0}};
int epo=0;
while(size!=0){
int num_sub=0;
for(i=0;i<size;i++){
int x=fresh_man[i][0];
int y=fresh_man[i][1];
for(j=0;j<4;j++){
int newx=x+drection[j][0];
int newy=y+drection[j][1];
if(newx>=0&&newx<n&&newy>=0&&newy<m){
if(grid[newx][newy]==2){
size--;
int tx=fresh_man[i][0];
int ty=fresh_man[i][1];
num_sub++;
fresh_man[i][0]=fresh_man[size][0];
fresh_man[i][1]=fresh_man[size][1];
fresh_man[size][0]=tx;
fresh_man[size][1]=ty;
i--;
break;
}
}
}
}
for(i=0;i<num_sub;i++){
grid[fresh_man[size+i][0]][fresh_man[size+i][1]]=2;
}
// printf("size %d numsub %d ",size,num_sub);
epo++;
if(num_sub==0){
break;
}
}
if(size!=0)return -1;
return epo;
}
边栏推荐
- mars3d点线面的绘制和重置
- Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
- How to view native IP
- 杂七杂八的东东
- JSON to proto
- Chrome删除重复书签
- 30 data visualization tips that can not be ignored
- 数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
- Data indicators and data analysis models that designers need to understand
- Give up Visio, this drawing tool is really fragrant!
猜你喜欢

haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)

Docker practice - redis cluster deployment and micro service deployment project

解析创客教育中的个性化学习进度

Gridsearchcv (grid search), a model parameter adjuster in sklearn

开源生态|超实用开源License基础知识扫盲帖(下)

Media industry under the epidemic situation, small program ecology driven digital transformation exploration

云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台

【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%

如何查看本机IP

ssm + ftp +ueditor
随机推荐
20220621 Dual Quaternion
Haas506 2.0 development tutorial - Advanced Component Library -modem Voicecall (only supports versions above 2.2)
Summary of qvariant use in QT
minio单节点部署 minio分布式部署 傻瓜式部署过程 (一)
Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation
json转化为proto
Programmers' real ideas | daily anecdotes
Problem: when the attribute in the data object (defined data) in the access component is also the attribute in the object object, an error is reported
疫情下的传媒产业,小程序生态驱动数字化转型探索
js 动态创建a href 循环下载文件只能下载10个或者固定数目的问题
【踩坑记录】数据库连接未关闭连接,释放资源的坑
下载oss文件并修改文件名
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
2022-01-12: give a positive array arr, length N, subscript 0~n-1, a
mysql 索引
SAP execution transaction code mrrl error -no message was found for partner 100065-
聚焦智慧城市,华为携手中科星图共同开拓数字化新蓝海
中台库存中的实仓与虚仓的业务逻辑设计
QT中的item views与Item widgets控件的用法总结
Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)