package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/find-all-groups-of-farmland/
 */
public class Problem1992 {
    public int[][] findFarmland(int[][] land) {
        // TODO
        return null;
    }

    private static void print(int[][] arr) {
        for (int[] a : arr) {
            System.out.println(Arrays.toString(a));
        }
    }

    public static void main(String[] args) {
        Problem1992 prob = new Problem1992();
        print(prob.findFarmland(new int[][]{
            {1,0,0},{0,1,1},{0,1,1}
        })); // [[0,0,0,0],[1,1,2,2]]
        print(prob.findFarmland(new int[][]{
            {1,1},{1,1}
        })); // [[0,0,1,1]]
        print(prob.findFarmland(new int[][]{
            {1,1},{0,0}
        })); // [[0,0,0,1]]
        print(prob.findFarmland(new int[][]{
            {0}
        })); // []
    }
}

@@ -34,17 +34,43 @@
 il.Array
-s
+List;%0Aimport java.util.List
 ;%0A%0A/**%0A 
@@ -221,85 +221,242 @@
     
-// TODO%0A        return null
+List%3Cint%5B%5D%3E farmlands = new ArrayList%3C%3E();%0A        int maxRow = land.length
 ;%0A    
-%7D%0A%0A
     
-private static void print(int%5B%5D%5B%5D arr
+int maxCol = land.length %3E 0 ? land%5B0%5D.length : 0;%0A        boolean%5B%5D%5B%5D visited = new boolean%5BmaxRow%5D%5BmaxCol%5D;%0A        for (int i = 0; i %3C maxRow; i++
 ) %7B%0A
@@ -463,24 +463,28 @@
         
+    
 for (int
 %5B%5D a : a
@@ -479,71 +479,359 @@
 (int
-%5B%5D a : arr) %7B%0A            System.out.println(Arrays.toString(a)
+ j = 0; j %3C maxCol; j++) %7B%0A                if (land%5Bi%5D%5Bj%5D == 0 %7C%7C visited%5Bi%5D%5Bj%5D) %7B%0A                    continue;%0A                %7D%0A                BottomRight bottomRight = new BottomRight();%0A                findFarmland(land, maxRow, maxCol, i, j, visited, bottomRight);%0A                farmlands.add(new int%5B%5D%7Bi, j, bottomRight.row, bottomRight.col%7D
 );%0A 
@@ -841,127 +841,334 @@
     
+  
-%7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1992 prob = new Problem1992();%0A        print(prob.
+  %7D%0A        %7D%0A        int%5B%5D%5B%5D answer = new int%5Bfarmlands.size()%5D%5B4%5D;%0A        for (int i = 0; i %3C farmlands.size(); i++) %7B%0A            answer%5Bi%5D = farmlands.get(i);%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static class BottomRight %7B%0A        private int row;%0A        private int col;%0A    %7D%0A%0A    private static void 
 find
@@ -1180,368 +1180,865 @@
 and(
-new 
 int%5B%5D%5B%5D
-%7B%0A            %7B1,0,0%7D,%7B0,1,1%7D,%7B0,1,1%7D%0A        %7D)); // %5B%5B0,0,0,0%5D,%5B1,1,2,2%5D%5D%0A        print(prob.findFarmland(new int%5B%5D%5B%5D%7B%0A            %7B1,1%7D,%7B1,1%7D%0A        %7D)); // %5B%5B0,0,1,1%5D%5D%0A        print(prob.findFarmland(new int%5B%5D%5B%5D%7B%0A            %7B1,1%7D,%7B0,0%7D%0A        %7D)); // %5B%5B0,0,0,1%5D%5D%0A        print(prob.findFarmland(new int%5B%5D%5B%5D%7B%0A            %7B0%7D%0A        %7D)); // %5B%5D
+ land, int maxRow, int maxCol,%0A                                     int row, int col, boolean%5B%5D%5B%5D visited,%0A                                     BottomRight bottomRight) %7B%0A        if (row %3C 0 %7C%7C row == maxRow %7C%7C col %3C 0 %7C%7C col == maxCol) %7B%0A            return;%0A        %7D%0A        if (land%5Brow%5D%5Bcol%5D == 0 %7C%7C visited%5Brow%5D%5Bcol%5D) %7B%0A            return;%0A        %7D%0A        if (row + col %3E= bottomRight.row + bottomRight.col) %7B%0A            bottomRight.row = row;%0A            bottomRight.col = col;%0A        %7D%0A        visited%5Brow%5D%5Bcol%5D = true;%0A        findFarmland(land, maxRow, maxCol, row - 1, col, visited, bottomRight);%0A        findFarmland(land, maxRow, maxCol, row, col + 1, visited, bottomRight);%0A        findFarmland(land, maxRow, maxCol, row + 1, col, visited, bottomRight);%0A        findFarmland(land, maxRow, maxCol, row, col - 1, visited, bottomRight);
 %0A   

Solve problem 1992