-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1992
48 lines (40 loc) · 1.77 KB
/
1992
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
class Solution {
public:
// Depth First Search (DFS) function to explore the land area
void dfs(int i, int j, vector<vector<int>>& a, vector<vector<int>>& v, int &ri, int &rj) {
// Mark the current cell as visited
v[i][j] = 1;
// Explore the neighboring cells in the down and right directions
if (i + 1 < a.size() && a[i + 1][j] == 1 && v[i + 1][j] == 0) {
// Update the bottom-right corner coordinates of the farmland area
ri = max(ri, i + 1);
dfs(i + 1, j, a, v, ri, rj);
}
if (j + 1 < a[0].size() && a[i][j + 1] == 1 && v[i][j + 1] == 0) {
// Update the bottom-right corner coordinates of the farmland area
rj = max(rj, j + 1);
dfs(i, j + 1, a, v, ri, rj);
}
}
// Function to find farmland areas in the given grid
vector<vector<int>> findFarmland(vector<vector<int>>& a) {
vector<vector<int>> ans;
int i, j;
// Create a 2D vector to keep track of visited cells
vector<vector<int>> v(a.size(), vector<int>(a[0].size(), 0));
// Iterate through each cell in the grid
for (i = 0; i < a.size(); i++) {
for (j = 0; j < a[0].size(); j++) {
// If the cell is not visited and represents farmland
if (!v[i][j] && a[i][j] == 1) {
int li = i, lj = j, ri = i, rj = j;
// Call DFS to explore the farmland area
dfs(i, j, a, v, ri, rj);
// Store the coordinates of the top-left and bottom-right corners of the farmland area
ans.push_back({li, lj, ri, rj});
}
}
}
return ans;
}
};