-
Notifications
You must be signed in to change notification settings - Fork 0
/
1992. Find All Groups of Farmland.cpp
37 lines (36 loc) · 1.11 KB
/
1992. Find All Groups of Farmland.cpp
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
class Solution {
public:
int m, n;
vector<vector<int>> result;
vector<vector<int>> directions{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void dfs(vector<vector<int>>& land, int i, int j, int& r2, int& c2) {
land[i][j] = 0;
r2 = max(r2, i);
c2 = max(c2, j);
for (auto& dir : directions) {
int new_i = i + dir[0];
int new_j = j + dir[1];
if (new_i >= 0 && new_i < m && new_j >= 0 && new_j < n &&
land[new_i][new_j] == 1) {
dfs(land, new_i, new_j, r2, c2);
}
}
}
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
m = land.size();
n = land[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (land[i][j] == 1) {
int r1 = i;
int c1 = j;
int r2 = -1;
int c2 = -1;
dfs(land, i, j, r2, c2);
result.push_back({r1, c1, r2, c2});
}
}
}
return result;
}
};