Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.Return the number of closed islands.Input FormatThe First line contains two integers denoting the value of n and mThe Next N lines contains M integers denoting the elements of the matrix.Constraints1 <= grid.length, grid[0].length <= 1000 <= grid[i][j] <=1Output FormatPrint a integer which is the answer to the question.Sample Input 03 50 0 1 0 00 1 0 1 00 1 1 1 0Sample Output 01Sample Input 13 41 0 1 1 1 1 1 1 1 1 1 1 Sample Output 10
Question
Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.Return the number of closed islands.Input FormatThe First line contains two integers denoting the value of n and mThe Next N lines contains M integers denoting the elements of the matrix.Constraints1 <= grid.length, grid[0].length <= 1000 <= grid[i][j] <=1Output FormatPrint a integer which is the answer to the question.Sample Input 03 50 0 1 0 00 1 0 1 00 1 1 1 0Sample Output 01Sample Input 13 41 0 1 1 1 1 1 1 1 1 1 1 Sample Output 10
Solution
The problem is asking to find the number of closed islands in a 2D grid. An island is a group of 0s (land) that are connected in the four directions (up, down, left, right). A closed island is an island that is completely surrounded by 1s (water).
Here are the steps to solve this problem:
-
Initialize a counter to keep track of the number of closed islands.
-
Iterate over the 2D grid. For each cell that contains a 0, perform a depth-first search (DFS) to visit all the cells in the same island. If the DFS visits a cell on the border of the grid, it means the island is not closed, so we should ignore it. If the DFS finishes without visiting a border cell, increment the counter.
-
To implement the DFS, use a stack or recursion. Start from the current cell and visit all its unvisited neighbors that contain a 0. To avoid visiting the same cell multiple times, mark it as visited after pushing it to the stack or calling the recursive function. You can mark a cell by changing its value to 1.
-
After visiting all the cells in the grid, the counter will be the number of closed islands.
Here is a Python solution for the problem:
def closedIsland(grid):
def dfs(i, j):
if not (0 <= i < len(grid)) or not (0 <= j < len(grid[0])) or grid[i][j] != 0:
return False
grid[i][j] = 1
return dfs(i-1, j) and dfs(i+1, j) and dfs(i, j-1) and dfs(i, j+1)
return sum(dfs(i, j) for i in range(len(grid)) for j in range(len(grid[0])) if grid[i][j] == 0)
This function first defines a helper function dfs to perform the depth-first search. Then it iterates over the grid and calls dfs for each cell that contains a 0. The dfs function returns True if the island is closed and False otherwise. The sum function counts the number of True values returned by dfs, which is the number of closed islands.
Similar Questions
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
200. Number of IslandsMediumTopicsCompaniesGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Given a 2D matrix of size NxN, print the sum of the elements of all its diagonals.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix. Each of the next N lines contains N integers - the elements of the matrix.Output FormatFor each test case, print the sum of the elements of all the diagonals, separated by a new line. Refer to samples for more clarity.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput43-5 0 42 8 -63 7 11-425 -2-4 16-2 -3 -6 -5 50 38 7 10 -5 -3 306 3 70 9 -20 -7-9 9 -6 7 3 2-1 7 7 6 -4 38 5 6 -9 40 8Output4 -6 4 9 3-4-2 6 -43 80 -15 -29 22 86 51 13 4 4 8ExplanationTest Case 1Sum of the elements of the 1st diagonal: 4Sum of the elements of the 2nd diagonal: 0 + -6 = -6Sum of the elements of the 3rd diagonal: -5 + 8 + 1 = 4Sum of the elements of the 4th diagonal: 2 + 7 = 9Sum of the elements of the 5th diagonal: 3Test Case 2Sum of the elements of the 1st and only diagonal: -4Test Case 3Sum of the elements of the 1st diagonal: -2Sum of the elements of the 2nd diagonal: 5 + 1 = 6Sum of the elements of the 3rd diagonal: -4
You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).Return true if all the cells satisfy these conditions, otherwise, return false. Example 1:Input: grid = [[1,0,2],[1,0,2]]Output: trueExplanation:All the cells in the grid satisfy the conditions.Example 2:Input: grid = [[1,1,1],[0,0,0]]Output: falseExplanation:All cells in the first row are equal.Example 3:Input: grid = [[1],[2],[3]]Output: falseExplanation:Cells in the first column have different values. Constraints:1 <= n, m <= 100 <= grid[i][j] <= 9
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.