首页 > 试题广场 >

腐烂的苹果

[编程题]腐烂的苹果
  • 热度指数:2786 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个 的网格,其中每个单元格中可能有三种值中的一个 0 , 1 , 2。
其中 0 表示这个格子为空、1 表示这个格子有一个完好的苹果,2 表示这个格子有一个腐烂的苹果。
腐烂的苹果每分钟会向上下左右四个方向的苹果传播一次病菌,并导致相邻的苹果腐烂。请问经过多少分钟,网格中不存在完好的苹果。如果有苹果永远不会腐烂则返回 -1。
数据范围: ,网格中的值满足
示例1

输入

[[2,1,1],[1,0,1],[1,1,1]]

输出

4
示例2

输入

[[2,1,0],[1,0,1],[0,0,0]]

输出

-1
头像 被困在学习算法的第一天一万年
发表于 2025-03-15 13:45:09
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param grid int整型vector< 展开全文
头像 万千少男的梦
发表于 2024-07-24 09:32:26
class Solution { int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int row, col; public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回 展开全文
头像 努涅斯不斜视
发表于 2025-01-21 22:41:43
import java.util.*; //多源bfs public class Solution { //用于访问四周节点 int[] dx = {0,0,1,-1}; int[] dy = {1,-1,0,0}; public int rotApple (Array 展开全文
头像 doreminomi
发表于 2023-11-23 17:51:58
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param grid int整型vector<vector<>> 展开全文
头像 可知春风过
发表于 2024-04-18 23:54:55
class Solution { int dx[4] = { 0,0,1,-1 }; int dy[4] = { 1,-1,0,0 }; bool visit[1010][1010] = { false }; public: // 多源 bfs + 最短路 展开全文
头像 上岸了的打工人很想提桶
发表于 2025-02-22 23:49:41
使用一个队列记录腐烂的苹果的位置....有点类似于二叉树的层序遍历.... import java.util.*; public class Solution { public int rotApple(ArrayList<ArrayList<Integer>> g 展开全文
头像 SilverBulletRye
发表于 2024-05-26 16:08:08
class Solution {//多源bfs public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param grid int整型vector<vector<>&g 展开全文
头像 17c89
发表于 2024-04-24 20:46:36
import java.util.*; /** * NC398 腐烂的苹果 * @author d3y1 */ public class Solution { private int ROW; private int COL; private int[] dx = n 展开全文
头像 奶ve
发表于 2024-05-12 09:13:35
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param grid int整型vector<vector<>> 展开全文
头像 smonkey1257
发表于 2024-04-22 14:36:59
class Solution { private: int row, col; // 记录矩阵的行、列 int dx[4] = { 0, 0, -1, 1 }; // 按照上、下、左、右的顺序BFS int dy[4] = { 1, 展开全文