-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1992FindAllGroupsOfFarmland.java
More file actions
50 lines (37 loc) · 1.39 KB
/
1992FindAllGroupsOfFarmland.java
File metadata and controls
50 lines (37 loc) · 1.39 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
//https://leetcode.com/problems/find-all-groups-of-farmland/
class Solution {
public int[][] findFarmland(int[][] land) {
List<Integer[]> answer = new ArrayList();
int rows = land.length, cols = land[0].length;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(land[i][j] == 1) {
Integer[] end = dfs(i, j, land, rows, cols);
answer.add(new Integer[]{i, j, end[0], end[1]});
}
}
}
int[][] result = new int[answer.size()][4];
for(int i = 0; i < answer.size(); i++) {
Integer[] group = answer.get(i);
result[i] = new int[]{group[0], group[1], group[2], group[3]};
}
return result;
}
Integer[] dfs(int i, int j, int[][] land, int rows, int cols) {
if(i < 0 || i >= rows || j < 0 || j >= cols || land[i][j] == 0) {
return null;
}
Integer[] res = new Integer[]{i, j};
land[i][j] = 0;
Integer[] right = dfs(i, j + 1, land, rows, cols);
if(right != null) {
res[1] = right[1];
}
Integer[] down = dfs(i + 1, j, land, rows, cols);
if(down != null) {
res[0] = down[0];
}
return res;
}
}