-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0994-Rotting-oranges.cs
More file actions
74 lines (61 loc) · 2.11 KB
/
0994-Rotting-oranges.cs
File metadata and controls
74 lines (61 loc) · 2.11 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0994.Rotting_oranges
{
public class _0994_Rotting_oranges
{
public int OrangesRotting(int[][] grid)
{
if (grid == null || grid.Length == 0) return -1;
var queue = new Queue<Tuple<int, int, int>>();
int row = grid.Length;
int col = grid[0].Length;
int minute = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (grid[i][j] == 2)
queue.Enqueue(new Tuple<int, int, int>(i, j, minute));
// BFS
while (queue.Count > 0)
{
var rotten = queue.Dequeue();
int x = rotten.Item1;
int y = rotten.Item2;
int time = rotten.Item3;
minute = Math.Max(minute, time);
time++;
// right
if (x < row - 1 && grid[x + 1][y] == 1)
{
queue.Enqueue(new Tuple<int, int, int>(x + 1, y, time));
grid[x + 1][y] = 2;
}
// left
if (x > 0 && grid[x - 1][y] == 1)
{
queue.Enqueue(new Tuple<int, int, int>(x - 1, y, time));
grid[x - 1][y] = 2;
}
// top
if (y > 0 && grid[x][y - 1] == 1)
{
queue.Enqueue(new Tuple<int, int, int>(x, y - 1, time));
grid[x][y - 1] = 2;
}
// bottom
if (y < col - 1 && grid[x][y + 1] == 1)
{
queue.Enqueue(new Tuple<int, int, int>(x, y + 1, time));
grid[x][y + 1] = 2;
}
}
// Check if all oranges are rotten.
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (grid[i][j] == 1)
return -1;
return minute;
}
}
}