#P3392. Minimum Painting for a Valid Flag
Minimum Painting for a Valid Flag
Minimum Painting for a Valid Flag
You are given a flag represented by an N × M grid. Each cell of the grid is colored white, blue, or red. A flag is considered valid if it is divided into three consecutive regions of rows:
- The top region (at least one row) is entirely white.
- The middle region (at least one row) is entirely blue.
- The bottom region (at least one row) is entirely red.
In other words, if we number the rows from 1 to N, there exist two indices i and j with 1 ≤ i < j < N such that:
\(\text{Rows } 1 \text{ to } i \text{ are white},\newline \text{Rows } i+1 \text{ to } j \text{ are blue},\newline \text{and Rows } j+1 \text{ to } N \text{ are red}.\)
You are allowed to repaint some cells (i.e. cover the existing color with a new one) so that the flag becomes valid. Your task is to determine the minimum number of cells that must be repainted.
inputFormat
The first line contains two integers N and M (3 ≤ N, M ≤ 50) — the number of rows and columns of the grid.
Then follow N lines, each containing a string of M characters. Each character is one of W
(white), B
(blue), or R
(red), representing the color of that cell.
outputFormat
Output a single integer — the minimum number of cells that need to be repainted for the flag to become valid.
sample
3 3
WWW
BBB
RRR
0
</p>