作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/rotting-oranges/

题目描述

n a given grid, each cell can have one of three values:

  • the value 0 representing an empty cell;
  • the value 1 representing a fresh orange;
  • the value 2 representing a rotten orange.

Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead.

Example 1:

Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4

Example 2:

Input: [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:

Input: [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Note:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. grid[i][j] is only 0, 1, or 2.

题目大意

在给定的二维格子里,每个格子有三个状态:0代表空,1代表新鲜的橘子,2代表腐败的橘子。

每一分钟,如果一个新鲜的橘子的四联通格子里有腐败的格子,那么这个格子的新鲜橘子就会变成腐败的。

问所有的橘子都腐败的时候,需要多久?

当不可能都腐败的时候,返回-1.

解题方法

BFS

其实这个题很简单的,就是类似的四联通地走迷宫。因为每一步都是四联通的向前走,所以我们使用BFS来解决。

首先统计新鲜橘子的个数,把腐败橘子的位置保存到队列中。

然后遍历队列,每一步中,把队列中已经有的所有腐败橘子都弹出来,判断它的四周有没有新鲜橘子,然后把新鲜橘子变成腐败的,并把该位置放到队列中,同时还要把新鲜橘子的个数-1.

当我们走到某一个时间之后,发现队列中没有腐败的橘子了。这个时候意味着在上一步中,没有新鲜橘子被传染成腐败的,即无路可走了。这个时候,我们停止。

停止之后,需要根据新鲜橘子的个数是不是已经全部被染成了腐败的来判断是不是返回-1.如果全部被染了,需要返回的是step - 1,为什么不是step呢?因为我们在对队列的第一次循环过程中,遍历了题目给出的腐败橘子,这个也统计到了step中,所以比要经历的时间多了1次,因此减去。

python代码如下:

class Solution(object):
def orangesRotting(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
M, N = len(grid), len(grid[0])
fresh = 0
q = collections.deque()
for i in range(M):
for j in range(N):
if grid[i][j] == 1:
fresh += 1
elif grid[i][j] == 2:
q.append((i, j))
if fresh == 0:
return 0
dirs = [(0, 1), (0, -1), (-1, 0), (1, 0)]
step = 0
while q:
size = len(q)
for i in range(size):
x, y = q.popleft()
for d in dirs:
nx, ny = x + d[0], y + d[1]
if nx < 0 or nx >= M or ny < 0 or ny >= N or grid[nx][ny] != 1:
continue
grid[nx][ny] = 2
q.append((nx, ny))
fresh -= 1
step += 1
if fresh != 0:
return -1
return step - 1

日期

2019 年 2 月 21 日 —— 一放假就再难抓紧了

【LeetCode】994. Rotting Oranges 解题报告(Python)的更多相关文章

  1. [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 ...

  2. [LeetCode] 994. Rotting Oranges 腐烂的橘子

    题目: 思路: 每个腐烂的橘子都能将自己上下左右的新鲜橘子传染,像极了现在的肺炎... 如果格子中只有一个腐烂的橘子,那么这便是一个典型的层次遍历,第一个传染多个,称为第二层,第二层传染第三层 但这里 ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【Leetcode_easy】994. Rotting Oranges

    problem 994. Rotting Oranges 参考 1. Leetcode_easy_994. Rotting Oranges; 完

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. nginx 文件目录页面

    开启文件目录 server { listen 80; #设置自己静态目录的访问域名 server_name xxx.xxxx.com; #防止页面中文乱码一定要在utf-8前面加上gbk,顺序很重要 ...

  2. bwa比对软件的使用以及其结果文件(sam)格式说明

    一.bwa比对软件的使用 1.对参考基因组构建索引 bwa index -a bwtsw hg19.fa   #  -a 参数:is[默认] or bwtsw,即bwa构建索引的两种算法,两种算法都是 ...

  3. JavaScript | 新手村(一)变量,运算和变量方法

    资料来自:JavaScript 第一步 1. 向 html 页面添加 JavaScript 1.1 内部 JavaScript 在 html 文件中的 </body> 标签前插入代码: & ...

  4. 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」

    作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...

  5. Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  6. mystar01 nodejs MVC 公共CSS,JS设置

    mystar01 nodejs MVC gulp 项目搭建 config/express.js中定义别名 //将下载的第三方库添加到静态资源路径当中,方便访问 app.use('/jquery', e ...

  7. 数据库SQL性能优化

    1.in与exists的效率比较 in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询.一直以来认为exists 比in 效率高的说法是不准 ...

  8. SpringMVC原理分析

    Spring MVC主要包括以下要点: 1:由DispatcherServlet控制的整个流程: 2:注解驱动的控制器,其中包括请求映射.数据的绑定和格式化: 3:文件上传: 4:一些杂项,如静态资源 ...

  9. Thymeleaf+layui+jquery复选框回显

    一.Thymeleaf+layui+jquery复选框回显 基于Thymeleaf模板下的layui+jquery复选框回显,主要是jquery.大致意思是:把数组转成JSON传到前台,再在前台转回数 ...

  10. mysql中索引,触发器,事务,存储引擎的理解

    网址:http://blog.51cto.com/760470897/1790460