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 ...
随机推荐
- IO多路复用多并发服务器模板
import socket import selectors # IO多路复用选择器 epoll_selector = selectors.EpollSelector() # 实例化选择器 serve ...
- HTML学习1-Dom之事件绑定
事件: 1.注册事件 a. <div onxxxx=””></div> b. document .onxxxx= function() //找到这个标签 2.this,触发 ...
- centos6.9+lnmp1.5环境部署swoole记录
hiredis下载地址:https://github.com/redis/hiredis/releasesunzip hiredis-v0.13.3.zipmake -jsudo make insta ...
- redis使用哈希槽实现集群
Redis Cluster集群 一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Re ...
- PyCharm配置SFTP远程调试Django应用
http://www.ithao123.cn/content-41747.html http://www.th7.cn/system/lin/201703/205998.shtml
- 第十次作业psp
psp 进度条 博文累积折线图 代码累积折线图 psp饼状图
- 奔跑吧DKY——团队Scrum冲刺阶段-Day 6
今日完成任务 谭鑫:制作相应动画人物,并实现人物动画 黄宇塘:制作相应动画人物,并实现人物动画,制作背景图 赵晓海:制作相应动画人物,并实现人物动画 方艺雯:制作相应动画人物,并实现人物动画,编写博客 ...
- 20172319 2018.03.27-04.05 《Java程序设计》第4周学习总结
20172319 2018.03.27-04.05 <Java程序设计>第4周学习总结 教材学习内容总结 第四章 编写类 类与对象的回顾:对象是有状态的,状态由对象的属性值确定.属性由类中 ...
- linux 环境配置要点
cd root .bash_profile 这个是配置当前用户的环境变量 cd /etcprofile 这个是配置系统的环境变量 which xxx 查看命令的目录 source .bash_prof ...
- docker删除为<none>的镜像
$ docker stop $(docker ps -a | grep "Exited" | awk '{print $1 }') //停止容器 $ docker rm ...