Dsa
Graphs Bfs
Easy
Island Explorer
You are given a grid representing a map of islands (1) and water (0). Each cell can represent either land or water. You need to determine how many distinct islands are present on the map. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Write a program that reads in the grid and returns the number of islands.
Input Format:
- The first line consists of two integers r and c, the number of rows and columns in the grid respectively.
- The next r lines contain c integers each (0 or 1) representing the grid.
Output Format:
- Print the number of distinct islands.
Example:
Input:
4 5
1 0 0 1 0
0 0 1 0 0
1 0 1 1 0
0 1 0 0 1
Output:
5
Key concepts
bfsgraphisland-count
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now