-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
56 lines (48 loc) · 1.42 KB
/
Copy pathsolution.cpp
File metadata and controls
56 lines (48 loc) · 1.42 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
class Solution
{
public:
int orangesRot(vector<vector<int>> &mat)
{
int n = mat.size(), m = mat[0].size();
queue<pair<int, int>> q;
int fresh = 0;
// Step 1: Store all rotten oranges & count fresh ones
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i][j] == 2)
q.push({i, j});
if (mat[i][j] == 1)
fresh++;
}
}
// Directions (up, down, left, right)
vector<int> dx = {-1, 1, 0, 0};
vector<int> dy = {0, 0, -1, 1};
int time = 0;
// Step 2: BFS
while (!q.empty() && fresh > 0)
{
int size = q.size();
time++; // one minute passes
for (int i = 0; i < size; i++)
{
auto [x, y] = q.front();
q.pop();
for (int d = 0; d < 4; d++)
{
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && mat[nx][ny] == 1)
{
mat[nx][ny] = 2;
q.push({nx, ny});
fresh--;
}
}
}
}
return fresh == 0 ? time : -1;
}
};