|
| 1 | +--- |
| 2 | +title: 994.Rotten orange.md |
| 3 | +date: 2024.05.14 0:00 |
| 4 | +tags: |
| 5 | + - Python |
| 6 | + - BFS |
| 7 | + - Bilateral queue |
| 8 | +abbrlink: 56e64fdd |
| 9 | +--- |
| 10 | + |
| 11 | +# topic: |
| 12 | + |
| 13 | +""" |
| 14 | +<p>Given <code>m x n</code> grid |
| 15 | + <meta charset="UTF-8" /> <code>grid</code> middle,Each cell can have one of the following three values:</p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li>value <code>0</code> Represents the empty unit;</li> |
| 19 | + <li>value <code>1</code> Represents fresh oranges;</li> |
| 20 | + <li>value <code>2</code> 代表Rotten orange。</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>every minute,Rotten orange <strong>around 4 Adjacent in this direction</strong> Fresh oranges will rot。</p> |
| 24 | + |
| 25 | +<p>return <em>直到单元格middle没有新鲜橘子为止所必须经过的最小分钟数。If it is impossible,return <code>-1</code></em> 。</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | + |
| 29 | +<p><strong>Exemplary example 1:</strong></p> |
| 30 | + |
| 31 | +<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/16/oranges.png" style="height: 137px; width: 650px;" /></strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>enter:</strong>grid = [[2,1,1],[1,1,0],[0,1,1]] |
| 35 | +<strong>Output:</strong>4 |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>Exemplary example 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>enter:</strong>grid = [[2,1,1],[0,1,1],[1,0,1]] |
| 42 | +<strong>Output:</strong>-1 |
| 43 | +<strong>explain:</strong>Orange in the lower left corner(First 2 OK, First 0 List)Never rot,Because rotten will only happen in 4 In the direction。 |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p><strong>Exemplary example 3:</strong></p> |
| 47 | + |
| 48 | +<pre> |
| 49 | +<strong>enter:</strong>grid = [[0,2]] |
| 50 | +<strong>Output:</strong>0 |
| 51 | +<strong>explain:</strong>because 0 There is no fresh orange in minutes,So the answer is 0 。 |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | + |
| 56 | +<p><strong>hint:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li><code>m == grid.length</code></li> |
| 60 | + <li><code>n == grid[i].length</code></li> |
| 61 | + <li><code>1 <= m, n <= 10</code></li> |
| 62 | + <li><code>grid[i][j]</code> Only for <code>0</code>、<code>1</code> or <code>2</code></li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +<div><div>Related Topics</div><div><li>Priority search</li><li>Array</li><li>matrix</li></div></div><br><div><li>👍 872</li><li>👎 0</li></div> |
| 66 | +""" |
| 67 | + |
| 68 | +# Thought: |
| 69 | + |
| 70 | +这个问题可以用Priority search(BFS)To solve。We need to track the spread of rotten oranges,Record time,And check if there is a fresh orange that cannot be rotten。The initial idea of the original idea: |
| 71 | +```python |
| 72 | +class Solution: |
| 73 | + def orangesRotting(self, grid: List[List[int]]) -> int: |
| 74 | + bad_orange = [] |
| 75 | + # 找到所有初始Rotten orange |
| 76 | + for i in range(len(grid)): |
| 77 | + for j in range(len(grid[0])): |
| 78 | + if grid[i][j] == 2: |
| 79 | + # 存入初始队List |
| 80 | + bad_orange.append((i, j)) |
| 81 | +``` |
| 82 | +Similar to multi -threaded,每个线程存入一个初始队List,初始队List通过BFSGradual diffusion |
| 83 | + |
| 84 | +# Code: |
| 85 | + |
| 86 | +```python |
| 87 | +from collections import deque |
| 88 | + |
| 89 | +class Solution: |
| 90 | + def orangesRotting(self, grid: List[List[int]]) -> int: |
| 91 | + bad_orange = deque() |
| 92 | + fresh_oranges = 0 |
| 93 | + rows, cols = len(grid), len(grid[0]) |
| 94 | + |
| 95 | + # 找到所有初始Rotten orange,And calculate the number of fresh oranges |
| 96 | + for i in range(rows): |
| 97 | + for j in range(cols): |
| 98 | + if grid[i][j] == 2: |
| 99 | + bad_orange.append((i, j)) |
| 100 | + elif grid[i][j] == 1: |
| 101 | + fresh_oranges += 1 |
| 102 | + |
| 103 | + # 方向Array:up down left right |
| 104 | + directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] |
| 105 | + |
| 106 | + # If there is no fresh orange,直接return 0 |
| 107 | + if fresh_oranges == 0: |
| 108 | + return 0 |
| 109 | + |
| 110 | + # BFS |
| 111 | + minutes = 0 |
| 112 | + while bad_orange: |
| 113 | + minutes += 1 |
| 114 | + for _ in range(len(bad_orange)): |
| 115 | + x, y = bad_orange.popleft() |
| 116 | + for dx, dy in directions: |
| 117 | + nx, ny = x + dx, y + dy |
| 118 | + if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] == 1: |
| 119 | + grid[nx][ny] = 2 |
| 120 | + fresh_oranges -= 1 |
| 121 | + bad_orange.append((nx, ny)) |
| 122 | + |
| 123 | + # If there are fresh oranges,return -1 |
| 124 | + return minutes - 1 if fresh_oranges == 0 else -1 |
| 125 | +``` |
| 126 | + |
| 127 | +```go |
| 128 | +import ( |
| 129 | + "sync" |
| 130 | +) |
| 131 | + |
| 132 | +func orangesRotting(grid [][]int) int { |
| 133 | + rows, cols := len(grid), len(grid[0]) |
| 134 | + badOranges := make([][2]int, 0) |
| 135 | + freshOranges := 0 |
| 136 | + |
| 137 | + // 找到所有初始Rotten orange,And calculate the number of fresh oranges |
| 138 | + for r := 0; r < rows; r++ { |
| 139 | + for c := 0; c < cols; c++ { |
| 140 | + if grid[r][c] == 2 { |
| 141 | + badOranges = append(badOranges, [2]int{r, c}) |
| 142 | + } else if grid[r][c] == 1 { |
| 143 | + freshOranges += 1 |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // If there is no fresh orange,直接return 0 |
| 149 | + if freshOranges == 0 { |
| 150 | + return 0 |
| 151 | + } |
| 152 | + |
| 153 | + directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} |
| 154 | + minutes := 0 |
| 155 | + |
| 156 | + var wg sync.WaitGroup |
| 157 | + |
| 158 | + // BFS |
| 159 | + for len(badOranges) > 0 { |
| 160 | + minutes++ |
| 161 | + nextBadOranges := make([][2]int, 0) |
| 162 | + for _, orange := range badOranges { |
| 163 | + x, y := orange[0], orange[1] |
| 164 | + wg.Add(1) |
| 165 | + go func(x, y int) { |
| 166 | + defer wg.Done() |
| 167 | + for _, d := range directions { |
| 168 | + nx, ny := x+d[0], y+d[1] |
| 169 | + if nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] == 1 { |
| 170 | + grid[nx][ny] = 2 |
| 171 | + nextBadOranges = append(nextBadOranges, [2]int{nx, ny}) |
| 172 | + freshOranges-- |
| 173 | + } |
| 174 | + } |
| 175 | + }(x, y) |
| 176 | + } |
| 177 | + wg.Wait() |
| 178 | + badOranges = nextBadOranges |
| 179 | + } |
| 180 | + |
| 181 | + // If there are fresh oranges,return -1 |
| 182 | + if freshOranges > 0 { |
| 183 | + return -1 |
| 184 | + } |
| 185 | + return minutes - 1 |
| 186 | +} |
| 187 | +``` |
0 commit comments