You are given a 0-indexedm x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.
To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.
land can be represented by a coordinate system where the top left corner of land is (0, 0) and the bottom right corner of land is (m-1, n-1). Find the coordinates of the top left and bottom right corner of each group of farmland. A group of farmland with a top left corner at (r1, c1) and a bottom right corner at (r2, c2) is represented by the 4-length array [r1, c1, r2, c2].
Return a 2D array containing the 4-length arrays described above for each group of farmland inland. If there are no groups of farmland, return an empty array. You may return the answer in any order.
Input: land =[[1,0,0],[0,1,1],[0,1,1]]Output: [[0,0,0,0],[1,1,2,2]]Explanation:
The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
The key idea is to use DFS to find the extent of each farmland group. When a farmland cell is found, expand to the right and down to find the bottom-right corner, marking all visited cells to avoid revisiting.
classSolution {
public: vector<vector<int>> findFarmland(vector<vector<int>>& land) {
int m = land.size(), n = land[0].size();
vector<vector<int>> ans;
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
if (land[i][j] ==1) {
int r2 = i, c2 = j;
// Expand down
while (r2 +1< m && land[r2 +1][j] ==1) ++r2;
// Expand right
while (c2 +1< n && land[i][c2 +1] ==1) ++c2;
// Mark all as visited
for (int x = i; x <= r2; ++x)
for (int y = j; y <= c2; ++y)
land[x][y] =0;
ans.push_back({i, j, r2, c2});
}
}
}
return ans;
}
};
classSolution {
publicint[][]findFarmland(int[][] land) {
int m = land.length, n = land[0].length;
List<int[]> ans =new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (land[i][j]== 1) {
int r2 = i, c2 = j;
while (r2 + 1 < m && land[r2 + 1][j]== 1) r2++;
while (c2 + 1 < n && land[i][c2 + 1]== 1) c2++;
for (int x = i; x <= r2; x++)
for (int y = j; y <= c2; y++)
land[x][y]= 0;
ans.add(newint[]{i, j, r2, c2});
}
}
}
return ans.toArray(newint[ans.size()][]);
}
}
classSolution {
funfindFarmland(land: Array<IntArray>): Array<IntArray> {
val m = land.size
val n = land[0].size
val ans = mutableListOf<IntArray>()
for (i in0 until m) {
for (j in0 until n) {
if (land[i][j] ==1) {
var r2 = i
var c2 = j
while (r2 + 1 < m && land[r2 + 1][j] ==1) r2++while (c2 + 1 < n && land[i][c2 + 1] ==1) c2++for (x in i..r2) for (y in j..c2) land[x][y] = 0 ans.add(intArrayOf(i, j, r2, c2))
}
}
}
return ans.toTypedArray()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
deffindFarmland(self, land: list[list[int]]) -> list[list[int]]:
m, n = len(land), len(land[0])
ans = []
for i in range(m):
for j in range(n):
if land[i][j] ==1:
r2, c2 = i, j
while r2 +1< m and land[r2 +1][j] ==1:
r2 +=1while c2 +1< n and land[i][c2 +1] ==1:
c2 +=1for x in range(i, r2 +1):
for y in range(j, c2 +1):
land[x][y] =0 ans.append([i, j, r2, c2])
return ans
impl Solution {
pubfnfind_farmland(mut land: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let m = land.len();
let n = land[0].len();
letmut ans =vec![];
for i in0..m {
for j in0..n {
if land[i][j] ==1 {
letmut r2 = i;
letmut c2 = j;
while r2 +1< m && land[r2 +1][j] ==1 { r2 +=1; }
while c2 +1< n && land[i][c2 +1] ==1 { c2 +=1; }
for x in i..=r2 {
for y in j..=c2 {
land[x][y] =0;
}
}
ans.push(vec![i asi32, j asi32, r2 asi32, c2 asi32]);
}
}
}
ans
}
}