当前位置:网站首页>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;
}
边栏推荐
- 30 data visualization tips that can not be ignored
- Summary of business logic security ideas
- 回调函数详解
- MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
- QT creator builds osgearth environment (osgqt msvc2017)
- Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
- 记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理
- Sklearn classification in sklearn_ Report & accuracy / recall /f1 value
- 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)
- haas506 2.0开发教程-高级组件库-modem.sim(仅支持2.2以上版本)
猜你喜欢

C# wpf 附加属性实现界面上定义装饰器

mysql 基础查询

Explain csma/cd, token bus and token ring clearly

解决挖矿病毒 sshd2(redis未设密码、清除crontab定时任务)

开源OAuth2框架 实现SSO单点登录

Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路

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

20220621 Dual Quaternion

如何查看本机IP

Vs+qt project transferred to QT Creator
随机推荐
Understand how learning JSX works
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)
SAP execution transaction code mrrl error -no message was found for partner 100065-
C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
回调函数详解
Topic35——34. 在排序数组中查找元素的第一个和最后一个位置
二叉树的遍历及相关知识
haas506 2.0开发教程-sntp(仅支持2.2以上版本)
C Advanced Learning -- Reflection
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
VS2013 FFMPEG环境配置及常见错误处理
ssm + ftp +ueditor
C# wpf 附加属性实现界面上定义装饰器
How to realize video call and live interaction in a small program when live broadcasting is so popular?
关于监督
华为软件测试笔试真题之变态逻辑推理题
解析创客教育中的个性化学习进度
使用ts-node直接运行TypeScript代码
Qt 中 QVariant 使用总结
json转化为proto