Rotting Oranges - LeetCode
题目链接
注意点
解法
解法一:bfs。首先先统计所有新鲜的橘子数目fresh
,如果fresh
大于0则一直执行bfs。我们只处理昨天刚腐烂的橘子,grid[i][j]
的值就表示第几天腐烂的橘子,由于新鲜橘子的值一开始就是2,所以每次修改的时候都要改为day+3
即 day+1+2
(2是本来就有的,1表示经过了一天)。如果bfs之后发现新鲜橘子数没有减少则return -1,否则继续下一天的腐烂。
class Solution {
public:
int bfs(vector<vector<int>>& grid,int x,int y,int day)
{
if(x < 0||y < 0||x >= grid.size()||y >= grid[x].size()||grid[x][y] != 1) return 0;
grid[x][y] = day+3;
return 1;
}
int orangesRotting(vector<vector<int>>& grid) {
int i,j,day = 0,fresh = 0;
for(i = 0;i < grid.size();i++)
{
for(j = 0;j < grid[i].size();j++)
{
if(grid[i][j] == 1) fresh += 1;
}
}
while(fresh > 0)
{
int old_fresh = fresh;
for(i = 0;i < grid.size();i++)
{
for(j = 0;j < grid[i].size();j++)
{
if(grid[i][j] == day+2)
{
fresh -= bfs(grid,i,j+1,day)+bfs(grid,i,j-1,day)+bfs(grid,i+1,j,day)+bfs(grid,i-1,j,day);
}
}
}
if(old_fresh == fresh) return -1;
day++;
}
return day;
}
};
小结
- bfs重要的是找好边界条件以及每次修改的值。
Rotting Oranges - LeetCode的更多相关文章
- Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)
Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- 【Leetcode_easy】994. Rotting Oranges
problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完
- 【LeetCode】994. Rotting Oranges 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetco ...
- 【leetcode】994. Rotting Oranges
题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...
- [leetcode] 994. Rotting Oranges
题目 You are given an m x n grid where each cell can have one of three values: 0 representing an empty ...
- [LeetCode] 994. Rotting Oranges 腐烂的橘子
题目: 思路: 每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎... 如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层 但这里 ...
- [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges
In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...
- [LeetCode] Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
随机推荐
- arp与rarp
- Netty源码分析第5章(ByteBuf)---->第7节: page级别的内存分配
Netty源码分析第五章: ByteBuf 第六节: page级别的内存分配 前面小节我们剖析过命中缓存的内存分配逻辑, 前提是如果缓存中有数据, 那么缓存中没有数据, netty是如何开辟一块内存进 ...
- ef5 数据库操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 图解Raid5数据存储的原理
- ORACLE中查询被锁定的表,以及如何解锁
http://www.cnblogs.com/weiyi1314/p/6813325.html
- servlet几个常用的方法
servlet继承了HTTPServlet所以可以重写父类的方法,下面一 一介绍方法Dopost DoGet 比较常用不再介绍. 一.Init(),和Init(ServletConfig config ...
- unique STL讲解和模板
unique()是C++标准库函数里面的函数,其功能是去除相邻的重复元素(只保留一个),所以使用前需要对数组进行排序. 代码: #include<bits/stdc++.h> using ...
- T4模板_T4基本结构
T4文本模板由 指令块.文本块.控制块 组成. 一. 指令块(MSDN文本模板指令) 指令块以@开头,基本的指令块包括<#@ template #> .<#@ parameter# ...
- C++操作mysql方法总结(3)
C++通过mysql++操作mysql的方式 使用vs2013和64位的msql 5.6.16进行操作 项目中使用的数据库名和表数据请参考C++操作mysql方法总结(1)中的介绍 Mysql ...
- APP端测试与web端测试的区别
想要知道APP端测试与web端测试的区别 ,那么我们就要先来了解,web和app的区别. web项目,一般都是b/s架构,基于浏览器的,而app则是c/s的,必须要有客户端.那么在系统测试测试的时候就 ...