994:腐烂的橘子

This commit is contained in:
huangge1199 2021-07-15 13:33:03 +08:00
parent 2f1e4e18c9
commit bb97e258c3
2 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,107 @@
//在给定的网格中每个单元格可以有以下三个值之一
//
//
// 0 代表空单元格
// 1 代表新鲜橘子
// 2 代表腐烂的橘子
//
//
// 每分钟任何与腐烂的橘子 4 个正方向上相邻的新鲜橘子都会腐烂
//
// 返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数如果不可能返回 -1
//
//
//
// 示例 1
//
//
//
// 输入[[2,1,1],[1,1,0],[0,1,1]]
//输出4
//
//
// 示例 2
//
// 输入[[2,1,1],[0,1,1],[1,0,1]]
//输出-1
//解释左下角的橘子 2 0 永远不会腐烂因为腐烂只会发生在 4 个正向上
//
//
// 示例 3
//
// 输入[[0,2]]
//输出0
//解释因为 0 分钟时已经没有新鲜橘子了所以答案就是 0
//
//
//
//
// 提示
//
//
// 1 <= grid.length <= 10
// 1 <= grid[0].length <= 10
// grid[i][j] 仅为 01 2
//
// Related Topics 广度优先搜索 数组 矩阵
// 👍 382 👎 0
package leetcode.editor.cn;
import java.util.*;
//994:腐烂的橘子
public class RottingOranges {
public static void main(String[] args) {
//测试代码
Solution solution = new RottingOranges().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int orangesRotting(int[][] grid) {
Queue<int[]> queue = new LinkedList<>();
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 2) {
queue.offer(new int[]{i, j});
} else if (grid[i][j] == 1) {
count++;
}
}
}
if (count == 0) {
return 0;
}
List<int[]> list = Arrays.asList(
new int[]{-1, 0},
new int[]{1, 0},
new int[]{0, -1},
new int[]{0, 1}
);
int[][] is = new int[grid.length][grid[0].length];
int time = -1;
while (!queue.isEmpty()) {
int[] arr = queue.poll();
for (int[] point : list) {
int x = arr[0] + point[0];
int y = arr[1] + point[1];
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != 1) {
continue;
}
is[x][y] = is[arr[0]][arr[1]] + 1;
time = Math.max(time, is[x][y]);
grid[x][y] = 2;
queue.offer(new int[]{x, y});
count--;
}
}
return count > 0 ? -1 : time;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,46 @@
<p>在给定的网格中,每个单元格可以有以下三个值之一:</p>
<ul>
<li>&nbsp;<code>0</code>&nbsp;代表空单元格;</li>
<li>&nbsp;<code>1</code>&nbsp;代表新鲜橘子;</li>
<li>&nbsp;<code>2</code>&nbsp;代表腐烂的橘子。</li>
</ul>
<p>每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂。</p>
<p>返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回&nbsp;<code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/16/oranges.png" style="height: 150px; width: 712px;"></strong></p>
<pre><strong>输入:</strong>[[2,1,1],[1,1,0],[0,1,1]]
<strong>输出:</strong>4
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>[[2,1,1],[0,1,1],[1,0,1]]
<strong>输出:</strong>-1
<strong>解释:</strong>左下角的橘子(第 2 行, 第 0 列)永远不会腐烂,因为腐烂只会发生在 4 个正向上。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>[[0,2]]
<strong>输出:</strong>0
<strong>解释:</strong>因为 0 分钟时已经没有新鲜橘子了,所以答案就是 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= grid.length &lt;= 10</code></li>
<li><code>1 &lt;= grid[0].length &lt;= 10</code></li>
<li><code>grid[i][j]</code> 仅为&nbsp;<code>0</code><code>1</code>&nbsp;&nbsp;<code>2</code></li>
</ol>
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 382</li><li>👎 0</li></div>