package leetcode;

/**
 * https://leetcode.com/problems/count-sub-islands/
 */
public class Problem1905 {
    public int countSubIslands(int[][] grid1, int[][] grid2) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1905 prob = new Problem1905();
        System.out.println(prob.countSubIslands(new int[][]{
            {1,1,1,0,0},
            {0,1,1,1,1},
            {0,0,0,0,0},
            {1,0,0,0,0},
            {1,1,0,1,1}
        }, new int[][]{
            {1,1,1,0,0},
            {0,0,1,1,1},
            {0,1,0,0,0},
            {1,0,1,1,0},
            {0,1,0,1,0}
        })); // 3
        System.out.println(prob.countSubIslands(new int[][]{
            {1,0,1,0,1},
            {1,1,1,1,1},
            {0,0,0,0,0},
            {1,1,1,1,1},
            {1,0,1,0,1}
        }, new int[][]{
            {0,0,0,0,0},
            {1,1,1,1,1},
            {0,1,0,1,0},
            {0,1,0,1,0},
            {1,0,0,0,1}
        })); // 2
    }
}

@@ -12,16 +12,116 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -274,32 +274,1780 @@
     
-// TODO%0A        return 0
+int answer = 0;%0A        List%3CSet%3CString%3E%3E islands1 = getIslands(grid1);%0A        List%3CSet%3CString%3E%3E islands2 = getIslands(grid2);%0A        for (Set%3CString%3E island1 : islands1) %7B%0A            for (Set%3CString%3E island2 : islands2) %7B%0A                if (island1.containsAll(island2)) %7B%0A                    answer++;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static List%3CSet%3CString%3E%3E getIslands(int%5B%5D%5B%5D grid) %7B%0A        List%3CSet%3CString%3E%3E islands = new ArrayList%3C%3E();%0A        int maxRow = grid.length;%0A        int maxCol = grid.length %3E 0 ? grid%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            for (int j = 0; j %3C maxCol; j++) %7B%0A                Set%3CString%3E island = new HashSet%3C%3E();%0A                getIsland(grid, maxRow, maxCol, visited, island, i, j);%0A                if (!island.isEmpty()) %7B%0A                    islands.add(island);%0A                %7D%0A            %7D%0A        %7D%0A        return islands;%0A    %7D%0A%0A    private static void getIsland(int%5B%5D%5B%5D grid, int maxRow, int maxCol, boolean%5B%5D%5B%5D visited,%0A                                  Set%3CString%3E island, int row, int col) %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 (grid%5Brow%5D%5Bcol%5D == 0) %7B%0A            return;%0A        %7D%0A        if (visited%5Brow%5D%5Bcol%5D) %7B%0A            return;%0A        %7D%0A        visited%5Brow%5D%5Bcol%5D = true;%0A        island.add(row + %22,%22 + col);%0A        getIsland(grid, maxRow, maxCol, visited, island, row - 1, col);%0A        getIsland(grid, maxRow, maxCol, visited, island, row, col + 1);%0A        getIsland(grid, maxRow, maxCol, visited, island, row + 1, col);%0A        getIsland(grid, maxRow, maxCol, visited, island, row, col - 1)
 ;%0A  

Update problem 1905