#P8662. Island Flooding
Island Flooding
Island Flooding
You are given an \(N \times N\) pixel photograph of a sea region. Each pixel is either a water cell denoted by .
or a land cell denoted by #
. A group of land cells that are connected horizontally or vertically (i.e., up, down, left, right) forms an island.
Due to global warming, scientists predict that in the coming decades, the border of each island (i.e. every land cell that has at least one adjacent water cell in the four directions) will be flooded by rising sea levels. In other words, if a land cell has at least one adjacent cell (up, down, left, or right) that is water, that cell will be submerged.
Your task is to determine how many islands in the photograph will be completely submerged (i.e., every land cell in the island is flooded) after this predicted flooding.
Note: When checking adjacent cells, only the four directions within the grid are considered.
Example:
Input: 7 ....... .##.... .##.... ....##. ..####. ...###. .......</p>After flooding, the sea region becomes: ....... ....... ....... ....... ....#.. ....... .......
In the above example, the photograph initially contains 2 islands. After flooding, the island on the top-left is completely submerged, while the second island still retains one surviving land cell. Thus, the answer is 1.
inputFormat
The first line contains an integer \(N\) (the size of the grid). The next \(N\) lines each contain a string of \(N\) characters, each being either .
(representing water) or #
(representing land).
For example:
7 ....... .##.... .##.... ....##. ..####. ...###. .......
outputFormat
Output a single integer, the number of islands that will be completely submerged after the predicted flooding.
sample
7
.......
.##....
.##....
....##.
..####.
...###.
.......
1