- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a grid, there are few 0s and few 1s. We have to count the number of islands. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.

Suppose the grid is like −

1 | 1 | 0 | 0 | 0 |

1 | 1 | 0 | 0 | 0 |

0 | 0 | 1 | 0 | 0 |

0 | 0 | 0 | 1 | 1 |

There are three islands.

To solve this, we will follow these steps −

- There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −
- if number of rows in the grid is 0, then return 0
- n = row count and m := column count, and ans := 0
- for i in range 0 to n – 1
- for j in range 0 to m
- if grid[i, j] = 1, then ans := ans + 1
- makeWater(i, j, n, m, grid)

- for j in range 0 to m
- the makeWater() will take the indices i, j, row and col count n and m, and grid
- if i <0 or j < 0 or i >= n or j >= m, then return from this method
- if grid[i, j] = 0, then return otherwise make grid[i, j] := 0
- call makeWater(i + 1, j, n, m, grid)
- call makeWater(i, j + 1, n, m, grid)

Let us see the following implementation to get better understanding −

class Solution(object): def numIslands(self, grid): if len(grid) == 0: return 0 n= len(grid) m = len(grid[0]) ans = 0 for i in range(n): for j in range(m): if grid[i][j] == "1": ans+=1 self.make_water(i,j,n,m,grid) return ans def make_water(self,i,j,n,m,grid): if i<0 or j<0 or i>=n or j>=m: return if grid[i][j] == "0": return else: grid[i][j]="0" self.make_water(i+1,j,n,m,grid) self.make_water(i,j+1,n,m,grid) self.make_water(i-1,j,n,m,grid) self.make_water(i,j-1,n,m,grid) ob1 = Solution() print(ob1.numIslands([["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"], ["0","0","0","1","1"]]))

[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]

3

- Related Questions & Answers
- Number of Closed Islands in C++
- Number of Distinct Islands in C++
- Number of Distinct Islands II in C++
- Find the number of distinct islands in a 2D matrix in Python
- Program to count number of islands in a given matrix in Python
- Program to count number of surrounded islands in the matrix in python
- Program to count number of overlapping islands in two maps in Python
- Program to find number of islands, from where we cannot leave in Python
- How to print number of islands in a given matrix using C#?
- Program to find distance of shortest bridge between islands in Python
- Program to remove all islands which are completely surrounded by water in Python
- Number of Boomerangs in Python
- Number of 1 Bits in Python
- Unique Number of Occurrences in Python
- Number of Recent Calls in Python

Advertisements