#C7711. Taco Sprinklers
Taco Sprinklers
Taco Sprinklers
You are given a grid of size N × M where each cell is either a sunflower ('S') or an empty cell ('.'). A sprinkler, when placed in an empty cell, waters the entire row and column it is in. The goal is to determine the minimum number of sprinklers required so that every sunflower is watered.
A sprinkler can only be placed in an empty cell. However, an optimal placement can be derived from the observation that every sunflower must be watered either by a sprinkler in its row or in its column. Thus, the answer can be computed as:
$$\min\Big(\Big|\{ i:\; \exists\, j \text{ such that } field[i][j]='S' \}\Big|, \; \Big|\{ j:\; \exists\, i \text{ such that } field[i][j]='S' \}\Big|\Big) $$If there are no sunflowers in the grid, the answer is 0.
inputFormat
The first line contains two integers N
and M
- the number of rows and columns in the grid. Each of the next N
lines contains M
space-separated characters, each either 'S' or '.', representing the grid.
outputFormat
Output a single integer, the minimum number of sprinklers required to water all sunflowers.
## sample4 4
. S . .
. . S .
. . . .
S . . .
3