# 994. Rotting Oranges 腐烂的橘子

@TOC

## # 题目描述

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.

## # 解题方法

### # BFS

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 日 —— 一放假就再难抓紧了