A popular problem associated with Leetcode’s online judge. Determine the count of islands in a grid of locations that are either land or water.
Consider the following grid of locations.
Each location is either land (1) or water (0).
Your task is to count the number of islands. An island is surrounded by water and consists of several pieces of land reachable from each other by moving horizontally or vertically. Whatever is outside the four edges of the grid is water.
Input. The input consists of the number of rows of the grid in one line followed by the rows of the grid. For example
The output consists of a single number, the count of islands in the grid. For example, the number of islands in the sample input is 3.
Here is a Ruby implementation.
Want to read more?
I love to explain and answer questions on programming problems, the kind you find in coding interviews. I publish a new programming problem and its solution every month. Did I mention that I love to answer questions?