Minimum number of islands
InterviewTime limit2sMemory limit512 MB
Given a grid of land, water, and cloud cells, find the minimum possible number of 4-connected land islands if every cloud can be either land or water.
- Level
Medium5 of 10
- Topics
- Graph, DFS, Greedy, Union-find
- Solved
- No attempts yet
Problem
You are mapping a faraway planet with a satellite. The planet's surface is a grid, and the satellite captured one image of that surface. Each grid cell is land (written as 'L'), water (written as 'W'), or covered by clouds (written as 'C'). A cell covered by clouds can be either land or water, and you cannot tell which.
An island is a region of land cells in which every two cells are connected to each other by a path that only moves up, down, left, or right.
Given the image, find the smallest number of islands that is consistent with it.
Input
The first line contains the number of rows and the number of columns of the image ().
Each of the next lines contains exactly characters. Every character is 'L' for land, 'W' for water, or 'C' for clouds.
Output
Print a single integer, the smallest possible number of islands.