#C11423. Distinct Islands Counting
Distinct Islands Counting
Distinct Islands Counting
You are given a grid representing a map where each cell is either land or water. Land is denoted by the character L
and water by W
. Two land cells are considered connected if they are adjacent horizontally or vertically (diagonals do not count).
The first line of the input contains two integers n
and m
which represent the number of rows and columns of the grid respectively. This is followed by n
lines, each containing a string of length m
(without spaces) that represents a row in the grid.
The goal is to count the number of distinct islands in the grid.
An island is a group of one or more L
cells connected together horizontally or vertically. In mathematical terms, if we denote the position of a cell by \((i,j)\), then two cells \((i_1, j_1)\) and \((i_2, j_2)\) are adjacent if and only if
\[
|i_1 - i_2| + |j_1 - j_2| = 1.
\]
inputFormat
The first line contains two space‐separated integers n
and m
(the number of rows and columns respectively).
The next n
lines each contain a string of length m
composed only of the characters L
and W
.
outputFormat
Output a single integer denoting the number of distinct islands in the grid.
## sample4 5
LLWLL
LWWLW
WWWLL
LLLWL
3