-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0994-rotting-oranges.c
43 lines (37 loc) · 1.24 KB
/
0994-rotting-oranges.c
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
int directions[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
bool rotting_process(int** grid, int rows, int cols, int timestamp) {
bool continue_process = false;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row][col] == timestamp) {
for (int i = 0; i < 4; i++) {
int r = row + directions[i][0];
int c = col + directions[i][1];
if (rows > r && r >= 0 && cols > c && c >= 0) {
if (grid[r][c] == 1) {
grid[r][c] = timestamp + 1;
continue_process = true;
}
}
}
}
}
}
return continue_process;
}
int orangesRotting(int** grid, int gridSize, int* gridColSize){
int rows = gridSize;
int cols = gridColSize[0];
int timestamp = 2;
while (rotting_process(grid, rows, cols, timestamp)) {
timestamp += 1;
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row][col] == 1) {
return -1;
}
}
}
return timestamp - 2;
}