The program to find the total number of islands using DFS is discussed here. Given an input island matrix, where 0 represents water and 1 represents land. Find the total number of islands that are formed by connected 1’s.
For example, consider the input island matrix
1 0 1 0 1
0 0 1 0 0
0 0 1 1 0
0 1 0 1 0
1 1 1 0 0
0 0 0 0 1
0 1 0 1 0
0 0 1 1 0
0 0 0 1 1
1 1 0 0 0
Total number of islands = 5
The program to find the number of islands using DFS is given below.
@@coding::1@@
Time complexity: O(n^2)
Recommended Programs