-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFarmlandGroups.cpp
56 lines (51 loc) · 1.7 KB
/
FarmlandGroups.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Problem: https://leetcode.com/problems/find-all-groups-of-farmland/
#include <vector>
using namespace std;
class FarmlandGroups {
public:
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
vector<vector<int>> result;
int max_rows = land.size();
int max_cols = land[0].size();
vector<int> indices;
indices.resize(4, 0);
unordered_set<string> seen;
for (int r = 0; r < max_rows; ++r) {
for (int c = 0; c < max_cols; ++c) {
if (land[r][c] == 0) continue;
string key = to_string(r) + ":" + to_string(c);
if (seen.find(key) != seen.end()) continue;
// land[r][c] == 1.
dfs(r, c, max_rows, max_cols, land, seen, indices);
result.push_back(indices);
}
}
return result;
}
private:
int getVal(int row, int col, int max_rows, int max_cols, vector<vector<int>>& land) {
if (row < 0 || col < 0 || row >= max_rows || col >= max_cols) return 0;
return land[row][col];
}
void dfs(int r, int c, int max_rows, int max_cols,
vector<vector<int>>& land, unordered_set<string>& seen,
vector<int>& indices) {
if (r >= max_rows || c >= max_cols || r < 0 || c < 0) return;
if (land[r][c] == 0) return;
string key = to_string(r) + ":" + to_string(c);
if (seen.find(key) != seen.end()) return;
seen.insert(key);
dfs(r + 1, c, max_rows, max_cols, land, seen, indices);
dfs(r, c + 1, max_rows, max_cols, land, seen, indices);
// Start
if ((getVal(r - 1, c, max_rows, max_cols, land) == 0) && (getVal(r, c - 1, max_rows, max_cols, land) == 0)) {
indices[0] = r;
indices[1] = c;
}
// End
if ((getVal(r + 1, c, max_rows, max_cols, land) == 0) && (getVal(r, c + 1, max_rows, max_cols, land) == 0)) {
indices[2] = r;
indices[3] = c;
}
}
};