#C12234. Island Analysis
Island Analysis
Island Analysis
You are given a two-dimensional grid (matrix) representing a map where each cell is either L
(land) or W
(water). Two cells are considered connected if they are adjacent horizontally or vertically. An island is defined as a maximal connected group of land cells.
Your task is to determine two quantities:
- The number of islands in the map.
- The size (i.e. the number of cells) of the largest island.
Mathematically, if we denote the set of all islands as \(\{I_1, I_2, \ldots, I_k\}\), then you need to compute:
\[ N = k \quad \text{and} \quad S = \max_{1 \le i \le k} |I_i| \]Implement a solution that reads the matrix from standard input and writes the results to standard output. Use an appropriate graph traversal algorithm (such as DFS or BFS) to solve the problem.
inputFormat
The first line contains two integers m
and n
representing the number of rows and columns of the grid respectively.
Each of the following m
lines contains n
characters separated by spaces, where each character is either L
(representing land) or W
(representing water).
outputFormat
Output two integers on separate lines:
- The first line should be the number of islands.
- The second line should be the size of the largest island.
3 3
L L L
L L L
L L L
1
9
</p>