package leetcode;

/**
 * https://leetcode.com/problems/number-of-closed-islands/
 */
public class Problem1254 {
    public int closedIsland(int[][] grid) {
        // TODO
        return 0;
    }

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

@@ -162,33 +162,1879 @@
     
-// TODO%0A        return 0;
+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 col = 0; col %3C maxCol; col++) %7B%0A            if (!visited%5B0%5D%5Bcol%5D && grid%5B0%5D%5Bcol%5D == 0) %7B%0A                dfs(grid, visited, maxRow, maxCol, 0, col);%0A            %7D%0A            if (!visited%5BmaxRow - 1%5D%5Bcol%5D && grid%5BmaxRow - 1%5D%5Bcol%5D == 0) %7B%0A                dfs(grid, visited, maxRow, maxCol, maxRow - 1, col);%0A            %7D%0A        %7D%0A        for (int row = 0; row %3C maxRow; row++) %7B%0A            if (!visited%5Brow%5D%5B0%5D && grid%5Brow%5D%5B0%5D == 0) %7B%0A                dfs(grid, visited, maxRow, maxCol, row, 0);%0A            %7D%0A            if (!visited%5Brow%5D%5BmaxCol - 1%5D && grid%5Brow%5D%5BmaxCol - 1%5D == 0) %7B%0A                dfs(grid, visited, maxRow, maxCol, row, maxCol - 1);%0A            %7D%0A        %7D%0A        int answer = 0;%0A        for (int row = 1; row %3C maxRow - 1; row++) %7B%0A            for (int col = 1; col %3C maxCol - 1; col++) %7B%0A                if (visited%5Brow%5D%5Bcol%5D %7C%7C grid%5Brow%5D%5Bcol%5D != 0) %7B%0A                    continue;%0A                %7D%0A                dfs(grid, visited, maxRow, maxCol, row, col);%0A                answer++;%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static void dfs(int%5B%5D%5B%5D grid, boolean%5B%5D%5B%5D visited,%0A                            int maxRow, int maxCol,%0A                            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 %7C%7C visited%5Brow%5D%5Bcol%5D) %7B%0A            return;%0A        %7D%0A        visited%5Brow%5D%5Bcol%5D = true;%0A        dfs(grid, visited, maxRow, maxCol, row - 1, col); // up%0A        dfs(grid, visited, maxRow, maxCol, row, col + 1); // right%0A        dfs(grid, visited, maxRow, maxCol, row + 1, col); // down%0A        dfs(grid, visited, maxRow, maxCol, row - 1, col); // left
 %0A   

Update problem 1254