forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0994-rotting-oranges.cs
51 lines (44 loc) · 1.54 KB
/
0994-rotting-oranges.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Solution {
public int OrangesRotting(int[][] grid)
{
if (grid == null || grid[0].Length == 0)
return 0;
int r = grid.Length, c = grid[0].Length, fresh = 0, time = 0;
Queue<(int, int)> q = new Queue<(int, int)>();
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (grid[i][j] == 1)
fresh++;
else if (grid[i][j] == 2)
{
q.Enqueue((i, j));
};
}
}
if (fresh == 0)
return 0;
int[,] dir = new int[,] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
while (q.Any())
{
time++;
int size = q.Count;
for (int i = 0; i < size; i++)
{
var curr = q.Dequeue();
for (int j = 0; j < 4; j++)
{
int row = curr.Item1 + dir[j, 0];
int col = curr.Item2 + dir[j, 1];
if (row >= 0 && row < r && col >= 0 && col < c && grid[row][col] == 1)
{
grid[row][col] = 2;
q.Enqueue((row, col));
fresh--;
}
}
}
}
return fresh == 0 ? time - 1 : -1;
}}