#K53817. Minimum Sprinkler Problem
Minimum Sprinkler Problem
Minimum Sprinkler Problem
You are given a rectangular garden represented as an n x m grid of characters. Each cell in the grid is either a plant represented by '*' or an empty cell represented by '.'. The garden is arranged in rows and columns. A sprinkler placed on the garden can water an entire row or an entire column. The goal is to determine the minimum number of sprinklers required to water every plant in the garden.
Note: Even if there are no plants in the garden, the answer is 0.
Core Idea: Identify the rows and columns that contain plants and the answer is the minimum of the count of such rows and columns, i.e., \(\min(\text{number of rows with plants}, \text{number of columns with plants})\).
inputFormat
The input is read from stdin
and consists of:
- The first line contains two integers n and m representing the number of rows and columns of the garden.
- Each of the following n lines contains a string of length m representing one row of the garden. Each character is either '*' (a plant) or '.' (an empty cell).
outputFormat
Output to stdout
a single integer which is the minimum number of sprinklers required to water all the plants in the garden.
3 4
.*..
..*.
....
2