当前位置:网站首页>994. rotten oranges - non recursive method
994. rotten oranges - non recursive method
2022-06-23 06:48:00 【Mr Gao】
994. Rotten oranges
In the given m x n grid grid in , Each cell can have one of three values :
value 0 For empty cells ;
value 1 For fresh oranges ;
value 2 For rotten oranges .
Every minute , Rotten oranges Around 4 Adjacent in two directions All fresh oranges rot .
return The minimum number of minutes that must elapse until there are no fresh oranges in the cell . If not , return -1 .
Example 1:
Input :grid = [[2,1,1],[1,1,0],[0,1,1]]
Output :4
Example 2:
Input :grid = [[2,1,1],[0,1,1],[1,0,1]]
Output :-1
explain : Orange in the lower left corner ( The first 2 That's ok , The first 0 Column ) Never rot , Because decay happens only in 4 Positive upward .
Example 3:
Input :grid = [[0,2]]
Output :0
explain : because 0 There are no fresh oranges by minute , So the answer is 0 .
The solution code is as follows :
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;
}
边栏推荐
- 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 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)
- 使用ts-node直接运行TypeScript代码
- WPF Command指令和INotifyPropertyChanged
- haas506 2.0开发教程-高级组件库-modem.sim(仅支持2.2以上版本)
- ffplay实现自定义输入流播放
- Synchronous switching power supply reduces EMI layout dv/dt di/dt
- Test of ers function under the supplier consignment purchase mode of SAP mm
- 开源OAuth2框架 实现SSO单点登录
- 反鸡汤致辞
猜你喜欢

xml dtd 记录

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

Qt 中 QVariant 使用总结

How to view native IP

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

剑指 Offer 42. 连续子数组的最大和

设计师需要懂的数据指标与数据分析模型

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

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

idea自动生成serialVersionUID
随机推荐
Copy and paste of idea without escape
2121. 相同元素的间隔之和-哈希表法
mingw-w64、msys和ffmpeg的配置与编译
Machine learning artifact scikit learn minimalist tutorial
解读创客教育中的团结协作精神
994. 腐烂的橘子-非递归法
解析创客教育中的造物原理
mongodb 记录
C语言运算符优先级口诀
总结的好处
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
XXL-SSO 实现SSO单点登录
MySQL ON DUPLICATE KEY 和 PgSQL ON CONFLICT(主键) 处理主键冲突
WPF Command指令和INotifyPropertyChanged
云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台
haas506 2.0开发教程-sntp(仅支持2.2以上版本)
Sword finger offer 42 Maximum sum of successive subarrays
C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)
Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation