We need to parse the problem: There's a chessboard initially all white. We can perform operations: choose a row or a column (i.e., a horizontal line of 8 squares or a vertical line of 8 squares) and paint all squares on that line black. Painting a square that's already black does nothing. We are given a target configuration (8x8 with B and W). It is guaranteed that the target can be achieved by some sequence of such strokes. We need to find the minimum number of strokes (rows + columns) needed to achieve it.
Observations: Each operation paints an entire row or column black. So final black squares are those that are covered by at least one row painted or at least one column painted. The order doesn't matter: the set of black squares is the union of the selected rows and columns. So the final black squares are exactly those squares that are either in a selected row or in a selected column.
Let R be set of rows (1..8) that we paint, and C be set of columns (1..8) that we paint. Then a square (i,j) is black if i∈R or j∈C, otherwise white.
Given a target matrix with some black squares and some white squares. For each white square (i,j), we must have i∉R and j∉C. For each black square (i,j), we must have i∈R or j∈C.
We need to choose R, C (subsets) such that these conditions hold, and minimize |R|+|C|.
The problem says it's guaranteed that some solution exists.
We need to compute the minimum number of strokes.
This is a classic set cover / bipartite covering problem. The constraints are tiny (8x8). So we can brute force: try all subsets of rows (2^8=256) and for each, determine which columns are forced? For each row subset, we can check consistency and compute minimal columns needed. Or we can formulate as a hitting set / vertex cover?
Let's think: The condition for white squares: For each white cell (i,j), i∉R and j∉C. So rows and columns that have any white cell cannot be selected if that white cell's other coordinate is also not selected? Wait: For a white cell (i,j), we require i∉R AND j∉C. So if we select row i, then that cell becomes black, which violates. Similarly if we select column j, it becomes black. Therefore, for each white cell, we must NOT select its row and NOT select its column. So all rows that contain a white cell cannot be selected? Not exactly: If a row contains a white cell at column j, we must not select that row, because if we select row i, then (i,j) would become black (since row i selected). However, is it possible that we also select column j? That wouldn't matter because row selection already makes it black. So to keep (i,j) white, we must ensure that neither row i nor column j is selected. So yes, row i cannot be selected if there exists any white cell in that row? Actually if row i contains any white cell at some column j, then to keep that white cell white, we must not select row i. But could we avoid selecting row i and also not select column j? That's necessary. So row i cannot be selected if there is at least one white cell in that row? Wait: Suppose row i has a white cell at column j. If we select row i, then (i,j) becomes black -> violation. So row i cannot be selected. So any row that contains a white cell must not be selected. Similarly, any column that contains a white cell must not be selected. Because if column j has a white cell at some row i, selecting column j would make that cell black.
Thus, the set of rows that can be selected is a subset of rows that have no white cells (i.e., are completely black in target). Similarly, columns that can be selected are a subset of columns that have no white cells (i.e., are completely black in target). However, is it possible that a row contains both black and white cells? In target, it could have both. If a row has any white cell, it can't be selected. So only rows that are all black can be selected. Similarly, only columns that are all black can be selected. But we also have to satisfy black cells: for each black cell, we need either its row selected or its column selected. Since we can only select rows that are all black and columns that are all black, we need to cover all black cells by selected rows/columns from among those that are all black. However, there might be black cells in rows/columns that have white cells; those rows/columns cannot be selected, so such black cells must be covered by the other dimension: e.g., a black cell in a row that has a white cell (thus row cannot be selected) must be covered by its column being selected, but that column must be all black to be selectable; if that column also has a white cell somewhere else, then it cannot be selected either, contradiction? Since the problem guarantees a solution, there must be a consistent assignment.
Let's formalize:
Define:
- RW = set of rows that contain at least one W.
- CW = set of columns that contain at least one W.
Then for any white cell (i,j), i∈RW, j∈CW. Moreover, since the cell is white, we require i∉R and j∉C. Thus, R ∩ RW = ∅, and C ∩ CW = ∅. So rows we can select are only from rows that are all black: let RB = rows with no W (i.e., rows where every cell is B). Similarly, columns we can select are only from columns with no W: CB = columns with no W.
Now, consider a black cell (i,j). It may be in a row that has W (i∈RW) or in a row that is all black (i∈RB). Similarly for column. Since rows in RW cannot be selected, if i∈RW, then we must have j∈C to cover that black cell. And since C only selects from CB, we must have j∈CB. So for any black cell in a row that has a white, its column must be selected, implying its column must be all black (CB). Similarly, if j∈CW, then i must be in R, implying i∈RB. So for a black cell, at least one of its row or column is selected. Since selected rows are only RB and selected columns only CB, the condition becomes: For each black cell (i,j), either (i∈RB and i∈R) OR (j∈CB and j∈C). But we may not select all RB rows; we choose subsets of RB and CB.
Thus we need to cover all black cells with selected rows (from RB) and selected columns (from CB). And we cannot select rows from RW or columns from CW. And we need to minimize |R|+|C|.
Now note: Black cells that are in rows RB and columns CB are covered if either row selected or column selected. Black cells that are in rows RW (i.e., row contains some white) must be covered by column selected, and that column must be in CB (since if column had white, it can't be selected). So these black cells force that column to be selected. Actually, if a black cell is in RW, then its row cannot be selected, so we must have its column selected. Therefore, for each black cell with i∈RW, we must have j∈C (selected). Since C ⊆ CB, j must be in CB and we must select it. So any column that has such a black cell must be selected. But careful: Could a column j that has a black cell from a row RW, but also has some white cell? If it had a white cell, then j∈CW, so cannot be selected. But then the black cell would be uncovered, contradiction. Therefore, in a feasible configuration, any black cell in a row with a white must have its column all black (i.e., column has no white). And similarly, any black cell in a column with a white must have its row all black. This is a consistency condition that the problem guarantees.
Thus, we can identify forced columns: columns that have at least one black cell in a row that is not all black (i.e., row contains W) must be selected. Similarly, forced rows: rows that have at least one black cell in a column that is not all black must be selected.
But we also have black cells where both row and column are all black (i.e., i∈RB, j∈CB). Those can be covered by either selecting the row or the column, giving flexibility.
So essentially, we have a bipartite covering problem: We have rows RB (all black rows) and columns CB (all black columns). Some of them are forced (must be selected) because of black cells that are in mixed rows/columns. Actually forced selection: For any row i∈RW, we cannot select it, but there may be black cells in those rows. Each such black cell forces selection of its column j. So the set of columns that are forced to be selected is: all columns j such that there exists a black cell (i,j) with i∈RW. But note: If column j is forced, it must be in CB (since otherwise impossible). So that's consistent.
Similarly, for any column j∈CW, we cannot select it, and any black cell in that column forces selection of its row i, which must be in RB.
After selecting these forced rows and columns, we have covered all black cells that involve at least one of RW or CW. The remaining black cells are those where i∈RB and j∈CB, and also those where both row and column are in the allowed sets (RB, CB). But we might have already covered some of them if either the row or column was forced. However, we still might need to cover the remaining ones that are not yet covered (i.e., cells where i∈RB and j∈CB, and we have not selected row i nor column j). We need to select additional rows/columns to cover them, minimizing total selections.
So the problem reduces to: Given sets RB and CB, with some rows and columns already forced selected (call R0 ⊆ RB, C0 ⊆ CB), we need to cover all remaining cells (i,j) that are black, i∈RB, j∈CB, and (i∉R0, j∉C0). Actually also need to consider if some cells are already covered by forced selections: if a row i is selected (forced or later), then all cells in that row are covered. Similarly for columns. So after forced selections, we consider uncovered black cells: those (i,j) such that i∉R_selected and j∉C_selected, and target cell is black (and also i∈RB, j∈CB necessarily, because if i∈RW or j∈CW, they are already forced covered by columns or rows respectively). Actually, check: If i∈RW (row has a white), we already forced that all such rows cannot be selected, but black cells in those rows are covered by forced column selections? We said for each black cell in RW, its column must be selected, so after selecting all forced columns, those cells are covered. But is it necessary that all such columns are selected? Yes, because if any such column not selected, that black cell would be uncovered. However, if there is a black cell in RW but its column also has a white (i.e., column ∈CW), then it's impossible to cover because column cannot be selected. So guaranteed that such cells do not exist. So after forced selections, all black cells with i∈RW or j∈CW are covered.
Thus only black cells left are those in RB x CB that are not yet covered by selected rows or columns. Now we need to cover them with minimal additional selections from RB \ R0 and CB \ C0.
This is essentially a set cover / hitting set problem: we have a bipartite graph where vertices are rows (RB) and columns (CB), edges correspond to black cells that need to be covered (i.e., those where neither row nor column is already selected). We need to select a set of vertices (rows or columns) such that every edge is incident to at least one selected vertex. That's a vertex cover problem on a bipartite graph. And we want minimum vertex cover, which by Kőnig's theorem equals the size of maximum matching. But note that we already have some rows/columns forced selected; those are like vertices that are already in the cover; we then need to cover the remaining edges with the other vertices. However, we could also decide to select additional rows/columns beyond the forced ones? But forced ones are mandatory; we cannot avoid them. So the total cover = forced rows + forced columns + minimum vertex cover of the subgraph induced by remaining rows and columns after removing forced ones? But careful: The forced selections are part of the cover, and they might cover some of these edges as well. Actually, we defined forced rows and columns based on covering cells involving RW/CW. Those forced selections might also cover some of the RB x CB black cells. So after selecting forced sets, we consider the subgraph of uncovered black cells: those cells where row not selected and column not selected. That's exactly the edges between rows in RB\R0 and columns in CB\C0 (since forced selected rows and columns are removed from consideration). So we need to cover these edges with minimum additional vertices from RB\R0 ∪ CB\C0. That's a standard minimum vertex cover on a bipartite graph (the subgraph). So total strokes = |R0| + |C0| + min_vertex_cover_size.
But wait: Could there be an alternative where we select fewer forced columns? The forced columns are deduced from necessity: For any black cell in a row that cannot be selected (RW), we must select its column; otherwise, that cell would be uncovered. So forced columns are mandatory. Similarly forced rows are mandatory. So the minimal solution must include all forced columns and forced rows. However, is it possible that by selecting additional rows (maybe from RB) we could avoid selecting some forced columns? Let's examine: A forced column comes from a black cell (i,j) where i∈RW. Since i∈RW, row i cannot be selected (because it contains a white cell). So we cannot cover that cell via its row. Therefore the only way to cover it is via its column. So indeed column must be selected. So forced columns are mandatory. Similarly forced rows are mandatory. So they must be in any solution.
Thus the reduction is correct.
Now we need to compute forced sets and then minimum vertex cover on the remaining bipartite graph.
But we must also consider a corner case: What if there are no white cells at all? Then the whole board is black. Then RW and CW are empty. So all rows are RB (8) and all columns CB (8). No forced rows/columns. Then we need to cover all cells (8x8) with rows and columns. But since all cells are black, we need to select a set of rows and columns such that every cell is covered. But note: The condition is: for every black cell (i,j), i∈R or j∈C. For all cells black, we need to cover all 64 cells. This is equivalent to selecting a set of rows and columns whose union covers the whole grid. The minimum number of rows+columns to cover all cells? Actually, we need that for each cell, either its row or its column is selected. That is equivalent to: The complement (the set of cells that are not covered) are those where both row not selected and column not selected. So we need that there is no cell with both row not selected and column not selected. That means the set of unselected rows and unselected columns must have no intersection? Actually, consider the set of rows not selected, call R', and columns not selected, call C'. Then uncovered cells would be R' x C'. We need no uncovered cells, i.e., R' x C' must be empty. That means either R' is empty or C' is empty. Because if there is at least one row not selected and one column not selected, then that combination gives a cell (that row, that column) which would be uncovered. So to cover all cells, we must either select all rows, or select all columns, or both? Actually, condition: R' x C' = ∅. This is equivalent to: either R' = ∅ (i.e., all rows selected) or C' = ∅ (all columns selected). Because if both R' and C' non-empty, then take any r∈R', c∈C' -> cell (r,c) exists and is uncovered. So the minimal total selections is min(|R|,|C|) where we choose either all rows (8) or all columns (8) or maybe a combination? Could we select some rows and some columns such that R' and C' are both nonempty but there are no cells? No, because any pair yields a cell. So indeed, either all rows selected, or all columns selected, or both, but minimal is 8? But is it possible to cover all cells with fewer than 8? Suppose we select 7 rows and 0 columns. Then R' = 1 row not selected, C' = all 8 columns not selected. Then the cell in that row and any column is uncovered: we have 8 uncovered cells, so fails. If we select 7 rows and 1 column, then R' = 1 row, C' = 7 columns, then the cell at that row and each of those 7 columns is uncovered (7 cells). So fails. So to cover all cells, we need either all rows selected or all columns selected. So minimum strokes = 8. But wait: Could there be a configuration with both some rows and some columns that yields coverage? Let's test: if we select rows set R and columns set C, uncovered cells are (not R) x (not C). To have no uncovered cells, we need that either not R is empty or not C is empty. So either R is all rows (R = all 8) or C is all columns (C = all 8). So indeed minimum = 8. But is that correct? In the problem, they said "It is allowed to paint a square many times". So we can paint rows and columns; after painting some rows and columns, any square that gets painted at least once is black. If we paint, say, row 1 and column 1, then square (1,1) is covered twice; but squares (1,j) for j≠1 are covered by row1; squares (i,1) for i≠1 covered by column1; but squares (i,j) for i≠1,j≠1 are uncovered and remain white. So to have all black, we need to cover all squares. So indeed either all rows or all columns.
But our reduction to vertex cover would give: Graph with rows RB and columns CB, all edges present (since all cells are black). We need a minimum vertex cover of the complete bipartite graph K_{8,8}. The minimum vertex cover size for a complete bipartite graph K_{m,n} is min(m,n) = 8? Actually, vertex cover: In a bipartite graph, min vertex cover = max matching. Max matching size in K_{8,8} is 8 (perfect matching). So min vertex cover size = 8. But we have no forced rows/columns, so total strokes = 8. That matches either all rows or all columns? Actually all rows gives 8, all columns gives 8. So vertex cover size 8 could be achieved by selecting all rows (size 8) or all columns (size 8). But is there a vertex cover of size 8 that is not all rows or all columns? In K_{8,8}, any vertex cover must have at least 8 vertices, and if it has exactly 8, it must be either all rows or all columns (or maybe a mix? For K_{2,2}, a vertex cover of size 2 could be one row and one column? Let's test: K_{2,2} edges: (r1,c1),(r1,c2),(r2,c1),(r2,c2). If we pick r1 and c1, then edges: (r1,c2) is covered by r1? Actually r1 is selected, so edge (r1,c2) covered; (r2,c1) covered by c1; (r2,c2) not covered because neither r2 nor c2 selected. So fails. So for K_{2,2}, minimum cover is 2, but the only covers of size 2 are both rows or both columns. In K_{8,8}, to cover all edges, you need at least 8 vertices; the only ways to get exactly 8 are either all rows or all columns? Could there be a mix? Suppose we select r rows and c columns, with r+c=8, r<8, c<8. Then uncovered rows = 8-r, uncovered columns = 8-c. Since r+c=8, uncovered rows + uncovered columns = (8-r)+(8-c)=16-(r+c)=8. If both uncovered rows and uncovered columns are >0, then there is at least one edge between them, which is uncovered. So to have no uncovered edges, either uncovered rows =0 or uncovered columns =0. So indeed r=8 or c=8. So vertex cover of size 8 is either all rows or all columns. So our method yields 8. Good.
Now we need to implement. The board is 8x8. Steps:
-
Parse input, create matrix.
-
Identify rows that contain at least one 'W' (RW). Similarly columns that contain at least one 'W' (CW).
- RB = rows with no W (all B). CB = columns with no W (all B).
-
Check forced selections:
- For each black cell (i,j) where i∈RW (row has W): then column j must be selected. So add j to forced columns set C0.
- For each black cell (i,j) where j∈CW (column has W): then row i must be selected. So add i to forced rows set R0.
However, we must be careful: A black cell might satisfy both conditions (i∈RW and j∈CW). Then both forced row? Actually if i∈RW and j∈CW, then both row and column cannot be selected (since RW and CW are prohibited). But then covering it is impossible because row can't be selected (due to white in row) and column can't be selected (due to white in column). So such configuration is infeasible, but problem guarantees it doesn't happen. So we can assume that for any black cell, at least one of i∉RW or j∉CW? Actually to be feasible, we cannot have both i∈RW and j∈CW simultaneously, because then row i can't be selected and column j can't be selected, making cell impossible to cover. So indeed, for all black cells, (i∈RW ∧ j∈CW) is false. That is, no black cell lies at the intersection of a row that has a white and a column that has a white. So either i∉RW or j∉CW (or both). So forced condition still holds: if i∈RW, then j must be selected (and thus j∉CW). So forced columns come from those black cells in rows with white; forced rows from black cells in columns with white.
However, what about black cells in rows with white but also the column has white? As argued, that cannot happen in feasible instance. So we are safe.
Also, forced columns might be duplicated; we just need set.
-
Verify that forced columns are all within CB (columns with no W). Similarly forced rows within RB. If not, then it's impossible, but guaranteed.
-
Now we have R0 ⊆ RB, C0 ⊆ CB. After selecting these, we need to consider remaining black cells that are not yet covered. Covered cells are those where either row in R0 or column in C0 (or both). The remaining black cells are those where row ∉ R0, column ∉ C0, and cell is black. Also note that these cells must have row in RB (since if row had W, it would be forced column, but then column would be in C0, so would be covered; however, if row in RW but column not in C0? But we said for such cell, column must be in C0, so it would be covered. So remaining black cells indeed have row in RB\R0 and column in CB\C0). So we can consider bipartite graph between rows RB' = RB \ R0 and columns CB' = CB \ C0, with an edge (i,j) if the cell (i,j) is black. Note: Some cells in RB' x CB' might already be covered if either row in R0 or column in C0, but since we removed those, they are not in RB' or CB'? Actually if row i ∈ R0, it's not in RB', so any cell with that row is covered. Similarly if column j ∈ C0, it's covered. So edges only between rows not in R0 and columns not in C0 represent cells that are not covered by forced selections.
But we must also consider that we might decide to select additional rows from RB' or columns from CB'. Possibly we could also select rows from RB that are not in RB'? Actually RB' = RB \ R0, so all rows in RB not forced. Similarly for columns. So yes.
Thus algorithm:
-
Parse board.
-
Compute rows_with_W: bool array rowW[8], true if any 'W'.
-
Compute colW[8] similarly.
-
Compute RB = rows all B (i.e., !rowW[i]).
-
Compute CB = columns all B (!colW[j]).
-
Initialize forcedRows = empty, forcedCols = empty.
-
For each cell (i,j) where board[i][j]=='B':
if rowW[i] and !colW[j]: then column j forced.
else if !rowW[i] and colW[j]: then row i forced.
else if rowW[i] and colW[j]: should not happen (infeasible), but we can ignore or treat as impossible (maybe output something else, but guaranteed not).
else if !rowW[i] and !colW[j]: then both row and column are all B; no immediate force.
However, note: For a black cell with rowW[i] true and colW[j] false, we need to force column j. But what about black cell with rowW[i] false and colW[j] false? No force.
Actually, we need to handle the forcedness: If rowW[i] is true, then row cannot be selected, so we must select column j. So we add j to forcedCols regardless of colW? But if colW[j] true, then column cannot be selected, contradiction; but guaranteed it won't happen. So we can add forced column if rowW[i] is true and board[i][j]=='B'. Similarly, if colW[j] is true, add forced row i. But careful: A black cell might have rowW true and colW false: we add forced column. Also if colW true and rowW false: add forced row. If both false: no force. If both true: impossible.
However, what if a black cell appears where rowW true and colW false, but column j might be forced multiple times. That's fine.
-
After processing all black cells, we have forcedRows and forcedCols as sets. But note: There is also possibility that forcedRows may include rows that are not in RB? By definition forced rows come from black cells with colW true and rowW false, so row is not in RW, thus row has no white, so it's in RB. Similarly forced columns are from rows with white, and column without white, so column is in CB. So they are subsets of RB and CB respectively.
-
Also, note: Could there be a scenario where a forced row and forced column conflict? No.
-
Next, we need to consider that forced rows and columns are mandatory. So we set R0 = forcedRows, C0 = forcedCols.
-
Now we need to consider uncovered black cells: iterate over all cells (i,j) where board[i][j]=='B'. Check if i∈R0 or j∈C0: if yes, covered. If not, then we add this edge to graph between i (if i∈RB) and j (if j∈CB). But i must be in RB? It could be that i is in RB because if i had white, then rowW[i] true, then this black cell would have forced column, and j would be in C0, so covered. So uncovered black cell must have rowW false (i.e., i∈RB) and colW false (j∈CB). Also not forced selected. So yes.
-
Build adjacency list for rows in RB\R0 and columns in CB\C0.
Example 1:
Input:
WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
Let's index rows 1-8, columns 1-8.
Row1: W W W B W W B W -> has W, so rowW[1]=true.
Row2: B B B B B B B B -> all B, rowW[2]=false.
Row3: WWWBWWBW (same as row1) -> rowW[3]=true.
Row4: same -> true.
Row5: true.
Row6: true.
Row7: true.
Row8: true.
So RB = rows with no W: only row2. So RB = {2}. All other rows have at least one W.
Columns:
Compute column wise:
col1: row1 W, row2 B, row3 W, row4 W, row5 W, row6 W, row7 W, row8 W -> has W (row1 is W), so colW[1]=true.
col2: W, B, W, W, W, W, W, W -> true.
col3: W, B, W, W, W, W, W, W -> true.
col4: B, B, B, B, B, B, B, B -> all B? Let's check: row1 B? Actually row1 col4 is B (since string "WWWB..." -> fourth char B). row2 col4 B, row3 col4 B, row4 col4 B, row5 col4 B, row6 col4 B, row7 col4 B, row8 col4 B -> all B, so colW[4]=false.
col5: W, B, W, W, W, W, W, W -> true.
col6: W, B, W, W, W, W, W, W -> true.
col7: B, B, B, B, B, B, B, B? Wait row1 col7 is B? Row1: "WWWBWWBW" - positions: 1:W,2:W,3:W,4:B,5:W,6:W,7:B,8:W. So col7: row1 B, row2 B, row3 B, row4 B, row5 B, row6 B, row7 B, row8 B -> all B, colW[7]=false.
col8: W, B, W, W, W, W, W, W -> true.
So columns with no W: col4, col7 are all B. So CB = {4,7}. Others have at least one W.
Now forced columns: For each black cell where row has W. Row2 is all B, but rowW[2]=false, so not forced from rows. For rows with W (all except row2), we look at black cells. For row1, black cells at col4, col7? Row1: col4 B, col7 B, others? also maybe other B? Row1 has B at col4 and col7. So for row1 (has W), these are black cells. So they force columns 4 and 7? But careful: col4 and col7 are CB (no W), so okay. Similarly row3: same pattern, also black at col4 and col7, force col4 and col7. Similarly rows 4-8: each has black at col4 and col7. So forced columns = {4,7}. Actually column 4 and column 7 are forced multiple times. So C0 = {4,7}.
Forced rows: For each black cell where column has W. Columns with W: 1,2,3,5,6,8. Look at black cells in those columns. Check column1: which rows have B? row2? Actually row2 col1 is B, and row2 is all B (no W), so rowW[2]=false. So for column1 (has W), black cell at (2,1) forces row2. Similarly column2: black at row2 forces row2. Column3: black at row2 forces row2. Column5: black at row2 forces row2. Column6: black at row2 forces row2. Column8: black at row2 forces row2. So forced rows = {2}. Also, are there any black cells in columns with W from other rows? For column1, row1? row1 col1 is W, not B. row3 col1 W, etc. So only row2 has B in columns with W. So forced rows = {2}. So R0 = {2}.
Now, forced rows and columns: R0 = {2}, C0 = {4,7}. But row2 is in RB, columns 4 and 7 in CB.
Now after selecting these, covered cells: any cell with row2 or column4 or column7 is covered.
Now what black cells remain uncovered? We need to consider black cells not covered. Let's list all black cells: The board has B at:
- Row2: all columns are B. But row2 is selected, so all those are covered.
- Row1: col4, col7 are B. Columns 4 and 7 are selected, so covered.
- Row3: col4, col7: covered.
- Row4: col4, col7: covered.
- Row5: col4, col7: covered.
- Row6: col4, col7: covered.
- Row7: col4, col7: covered.
- Row8: col4, col7: covered.
Thus all black cells are covered already! So no uncovered black cells remain. So the graph of uncovered cells is empty. So max matching size = 0. Then total strokes = |R0| + |C0| + 0 = 1 + 2 = 3. That matches the example output 3. Good.
Example 2:
Input:
WWWWWWWW
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
Row1: all W -> rowW[1]=true.
Row2: all B -> rowW[2]=false.
Rows3-8: all W -> true.
So RB = {2}. Others have W.
Columns: column1: row1 W, row2 B, row3 W,... So has W, colW[1]=true. Same for all columns? Since all rows except row2 are W, and row2 is B, each column has at least one W (from row1, etc.) So all columns have W: colW[1..8]=true. So CB = empty (no column is all B). Actually columns with no W would require all rows B, but row2 is B but other rows are W, so each column has W from row1 etc. So CB = ∅.
Forced columns: For black cells in rows with W. Row1? row1 has no black cells. Rows3-8 have no black cells. Row2 is all B, but rowW[2]=false, so not forced from rows. So forced columns: none? Actually need to check: for each black cell where rowW is true. Row2 has rowW false, so not. Rows with W have no black cells. So C0 = ∅.
Forced rows: For each black cell where colW is true. All columns have W, and row2 has B in each column. So for each column j, black cell (2,j) forces row2. So forced rows = {2}. So R0 = {2}.
Now after selecting row2, covered cells: row2 all columns covered. Also, forced columns none. Any other black cells? No, only row2 has B. So all covered. Uncovered black cells? None. So additional needed = 0. Total = |R0|+|C0| = 1 + 0 = 1. That matches example output 1.
Thus our approach works.
Now we must consider other edge cases.
Edge case: All white board. Then target all W. Then what is the minimal strokes? Since all squares white, we must not select any row or column that would turn any white black. For each white cell (i,j), we need i∉R and j∉C. Since all cells are white, this implies for all i,j: i∉R and j∉C. So R and C must be empty. So strokes = 0. Let's see if our method yields 0.
All white: rowW? Actually rowW[i] = true because there is W? Wait rowW means row contains at least one W. Since all cells are W, each row contains W, so rowW[i]=true for all i. Similarly colW[j]=true for all j. RB = rows with no W => none. CB = none. forced columns: for each black cell? There are no black cells, so forced columns and rows are empty. So R0 = C0 = ∅. Then uncovered black cells: none. So total = 0. Good.
Edge case: Some mixture.
Now what about a scenario where there are forced rows/columns, but also uncovered black cells that require a vertex cover. Let's test a simple case: Suppose only one black cell at (1,1), and the rest white. So board: B at (1,1), others W. This is feasible? Check: To have only (1,1) black and others white, we need to paint either row1 or column1, but not both because that would paint other cells. But if we paint row1, then (1,2)...(1,8) become black, not allowed. If we paint column1, then (2,1)...(8,1) become black. So it's impossible to have only one black cell. Indeed, the problem guarantees feasible inputs, so such board would not appear. So indeed any feasible configuration must satisfy certain constraints: If a row contains a black cell, and there is a white cell in same row, then that row cannot be selected, so the black cell must be covered by its column, which then must be all black. So the column must be all black. So the pattern is constrained.
Our algorithm yields: For the board with only (1,1) B, row1 has W (since other cells are W), so rowW[1]=true; column1 has W (since other rows are W), colW[1]=true. Then RB = rows all B? None, because row1 has W; other rows have W (since (2,1) W etc). CB = columns all B? column1 has W, others have W? Actually column1 has B at (1,1) but also W at others, so colW[1]=true. So both row and column have white, and black cell is at intersection, which is rowW true and colW true -> infeasible. Since guaranteed feasible, we don't need to handle.
Another test: Suppose row1 is all black, rest all white. Then board: row1 all B, rows2-8 all W. Is this feasible? We need row1 all black, others all white. To achieve, we could paint row1. That would make row1 all black, and no other rows/columns. But would that affect other cells? Painting row1 paints (1,1)...(1,8) black. That's fine. It does not affect other rows. So that yields row1 black, others white. That's feasible. Let's see our algorithm: row1: all B, so rowW[1]=false (since no W). rows2-8: have W, so rowW[2..8]=true. columns: column1 has B at row1 and W at rows2-8 => colW[1]=true. Similarly all columns have W from rows2-8, so colW[1..8]=true. So RB = {1}. CB = empty (since each column has at least one W). forced columns: black cells in rows with W? rows2-8 have no black cells. So none. forced rows: black cells in columns with W? Each column has W, and black cells exist at (1,j) for all j. Since colW[j]=true, each black cell forces row1. So forced rows = {1}. So R0={1}, C0 empty. After selecting row1, all black cells covered (since row1 selected). Uncovered black cells? None. Total strokes = 1. That matches expectation: just paint row1.
What if we had column1 all black, others white. Then similarly forced columns, strokes=1.
Now consider a board where we have a cross: row1 all black, column1 all black, and the rest white except perhaps (1,1) counted twice. This pattern: row1 all B, column1 all B, others white. Is that feasible? Let's check: To get row1 all B, we could paint row1. To get column1 all B, we could paint column1. But then (1,1) is black anyway. However, painting row1 and column1 would also paint (1,j) for j>1 and (i,1) for i>1, making those black, which are supposed to be white. So not feasible. So such configuration is not feasible unless those other cells are also black. So indeed, feasible configurations satisfy that if we have a black cell in a row that has a white, the column must be all black, etc.
Now, suppose we have a board where RB = {1,2}, CB = {3,4}, and there are black cells at (1,3),(1,4),(2,3) but (2,4) is white. Let's see if that is feasible. But white cell (2,4) is in row2 (which is all black? Wait row2 is in RB meaning row2 has no white? Actually RB means rows with no white. If row2 is in RB, it means row2 has no white cells. But we said (2,4) is white, contradiction. So not possible. So in RB rows, all cells must be black. So if a row is in RB (no white), then all cells in that row are black. Similarly, columns in CB are all black. So in the remaining subgraph (RB x CB), all cells are black? Let's verify: If a row is in RB, it has no white, meaning every cell in that row is B. So for any i∈RB, for all columns j (1..8), board[i][j] = B. Similarly, if a column is in CB, then for all rows i, board[i][j] = B. But note: It's possible that a row is in RB (all black) and a column is in CB (all black). Then all cells are black anyway, but not all cells necessarily; wait, if row i is all black, then for any column j, (i,j) is black, regardless of column's status. So indeed, for any row i∈RB, all cells are black. So in particular, for any j, (i,j) is black. Similarly for any column j∈CB, all cells are black. Therefore, in the submatrix RB x (all columns) it's all black. However, the cells in RB x (columns not in CB) are also black because row is all black. But those columns not in CB have at least one white somewhere (in some other row). But that's okay. For our uncovered black cells, we only care about cells that are black and not covered by forced selections. But note: In rows RB, all cells are black. So there are many black cells. But some of them might be covered already by forced columns. Forced columns are those that are forced because they have black cells in rows with white? Actually forced columns are from black cells in RW rows. Those forced columns must be in CB (since they cannot have white). So forced columns are subset of CB. After selecting forced columns, all cells in RB rows that are in those forced columns are covered. But there remain RB rows and columns not forced. But note: Columns not forced might be either in CB (all black) or not. But if a column is not in CB, it contains at least one white. Could such a column be selected? No, because columns with white cannot be selected (C ∩ CW = ∅). So only columns in CB are selectable. So columns not in CB cannot be selected. So in the uncovered set, columns must be in CB\C0. So those columns are all black columns. And rows in RB\R0 are all black rows. So the subgraph of uncovered cells consists of all cells between RB\R0 and CB\C0, because for any i∈RB\R0 and j∈CB\C0, (i,j) is black (since row i is all black). So edges are complete bipartite between these sets? Actually yes: For any i in RB (all black rows), for any j, (i,j) is black. In particular for j in CB\C0, it's black. So the uncovered black cells are exactly the Cartesian product (RB\R0) × (CB\C0). Because all those cells are black and not covered by forced rows/columns (since row not selected, column not selected). Also there could be black cells in rows RW? Those are covered by forced columns. Black cells in columns CW? Covered by forced rows. So the graph is complete bipartite between RB' and CB'.
Thus the uncovered black cells always form a complete bipartite graph between the remaining all-black rows and all-black columns, because rows in RB are completely black, and columns in CB are completely black, but note: a column in CB is all black, but a row in RB is all black; so their intersection is black. Also cells in RB rows but columns not in CB are black too, but those columns are not selectable and are not in CB', so they are not part of the graph because we cannot select those columns; but do we need to cover those cells? Those cells are black, and they must be covered by either row or column. But if the column is not selectable (because it has white), then the only way to cover those cells is via the row. But the row might be selected or not. Wait, let's reconsider: Our earlier deduction said that columns with white (CW) cannot be selected. But black cells that lie in such columns must be covered by their rows. And those rows must be all black (RB) because if the row also had white, it would be impossible. So those black cells in columns with white are forced to be covered by selecting the row. That row becomes forced. Indeed, our forced rows set includes rows that have black cells in columns with white. So after forcing those rows, all such cells are covered. But what about a column not in CB (i.e., has white) and a row that is not forced? Could there be a black cell in such a column and a row that is not forced? Suppose column j has white (i.e., there exists some row where it's white). For a black cell (i,j) to exist, row i must be all black? Actually if column j has a white cell at some row k, that row k could be different from i. So column j has at least one white cell, but could have black cells elsewhere. For a black cell (i,j) with column j having white, to cover it we must select row i. So row i must be selected. So that forces row i to be selected. Thus any black cell in a column with white forces its row to be selected. So after processing forced rows, all such rows are selected. So any column with white that has any black cell forces the rows of those black cells. If a column with white has no black cells (i.e., all rows are white? Actually column has white, but could it be all white? That would be column with only white, no black, then it's not in CB and not forced because there are no black cells. That's fine.)
Thus after forced selections, any remaining black cells must be in columns that have no white (i.e., columns in CB) and rows that have no white (i.e., rows in RB). Because if a column had white, any black cell in it would force its row, making that row selected (forced). Similarly, if a row had white, any black cell in it would force its column, making that column selected. So indeed, after forced selections, any black cell not covered lies in RB x CB. And as argued, rows in RB are all black, so for any column in CB, the cell is black. Also, there could be black cells in RB rows and columns not in CB that are not covered? But those columns have white; if they had any black cell, they would force the row. Since the row is RB, it's selectable; but if the row is not forced? Actually if column has white and there is a black cell (i,j) with i in RB, then that black cell forces row i to be selected. So row i would be in forced rows. So after including forced rows, such rows are selected, covering that black cell. So yes, all black cells in columns with white are covered by forced rows. So uncovered black cells are only those in RB x CB that are not covered by any forced row or column. Since forced rows and columns are subsets of RB and CB respectively, the uncovered cells are precisely (RB \ R0) x (CB \ C0). So indeed it's complete bipartite.
Thus the subgraph we need to cover is complete bipartite between RB' and CB'. That simplifies the problem: minimum vertex cover in a complete bipartite graph K_{m,n} is min(m, n). Actually the minimum vertex cover size for a complete bipartite graph K_{m,n} is min(m,n). Because you can cover all edges by selecting all vertices on the smaller side. But is that always minimal? In K_{m,n}, any vertex cover must include all vertices of one side unless you select some from both. Actually the minimum vertex cover size is min(m, n) because you can take all vertices of the smaller side; that covers all edges. Could there be a smaller cover? If m>0 and n>0, you need at least min(m,n) because each vertex covers at most max(m,n) edges, but the standard result: the size of minimum vertex cover in a complete bipartite graph is min(m,n). Indeed, by König's theorem, maximum matching size is min(m,n), so min vertex cover size = min(m,n). So the additional strokes needed is min(m, n) where m = |RB'|, n = |CB'|.
But is it always complete bipartite? Let's double-check: Could there be a scenario where some cell in RB' x CB' is white? Suppose row i is all black (RB) means no white in that row. So for any column j, (i,j) is black. So yes, all cells in RB x all columns are black. But what about columns in CB? They are all black as well. So any intersection is black. So indeed the subgraph is complete bipartite. Therefore, the uncovered black cells are exactly all pairs between RB' and CB'.
Thus the vertex cover for this complete bipartite graph is min(|RB'|, |CB'|). So additional strokes = min(m,n). Then total strokes = |R0| + |C0| + min(m, n).
But is this always correct? Consider forced rows and forced columns covering some of these cells. The uncovered set is (RB\R0) x (CB\C0). So m = number of all-black rows not selected, n = number of all-black columns not selected. To cover all edges between them, we need to select either all those rows or all those columns, or a mix that covers all. Minimum vertex cover for complete bipartite is min(m,n). So yes.
Thus we have a simple formula:
Let:
- R_B = set of rows with no W.
- C_B = set of columns with no W.
- For each black cell (i,j):
if row i has W (i not in R_B) then column j must be selected. But column j must be in C_B (otherwise impossible). So forced columns = set of columns j for which there exists a black cell in a row that has a W. However, note that if a column j is forced, it will be selected, and it belongs to C_B.
Similarly, if column j has W (j not in C_B) then row i must be selected, and row i must be in R_B.
- So forced rows = set of rows i for which there exists a black cell in a column that has a W.
- Let R0 = forced rows, C0 = forced columns.
- Then let R_remaining = R_B \ R0, C_remaining = C_B \ C0.
- Then answer = |R0| + |C0| + min(|R_remaining|, |C_remaining|).
But careful: Could there be rows in R_B that also have forced rows from black cells in columns with W? That's already included. Could there be rows that are forced multiple times? Fine.
But we must also consider that forced rows and forced columns may overlap? They are disjoint because rows are from R_B, columns from C_B. So no overlap.
Now, we must also consider if there are any rows in R_B that are not forced, but they might need to be selected as part of covering the complete bipartite? Yes, that's captured by min.
Thus the answer is simply: count of forced rows + forced columns + min(#all-black rows not forced, #all-black columns not forced).
But is it always optimal to not select any forced rows/columns beyond those forced? Yes, forced are necessary. And for the remaining, we can either select all remaining rows or all remaining columns, whichever is smaller. Could there be a scenario where selecting a mix yields fewer than min(m,n)? For a complete bipartite graph, the minimum vertex cover is min(m,n), and it's achieved by taking all vertices on the smaller side. So that yields either all remaining rows or all remaining columns. So additional strokes = min(m,n). Good.
Now we need to be careful: Is it always true that after forced selections, the uncovered black cells are exactly the Cartesian product? Let's prove: After selecting forced rows R0 and forced columns C0, which are subsets of R_B and C_B respectively. Any black cell (i,j) that is not covered must have i∉R0 and j∉C0. Also it must satisfy that i∉RW and j∉CW because if i∈RW, then column j would have been forced, so j∈C0, contradicting j∉C0. Similarly if j∈CW then i∈R0. So i∈R_B and j∈C_B. Also, any i∈R_B and j∈C_B, the cell (i,j) is black because row i is all black. So indeed all such cells are black. So the uncovered set is R_B\R0 × C_B\C0. So it's complete bipartite.
Thus answer formula holds.
Now we need to compute forced rows/columns correctly. But we need to ensure we don't double-count or miss any. Let's derive from constraints.
Simplify: Compute:
-
row_all_black[i] = true if row i has no W.
-
col_all_black[j] = true if col j has no W.
-
forced_rows = set of rows i such that there exists j with board[i][j]=='B' and not col_all_black[j] (i.e., column has W). Because if column has white, then to cover that black cell, row i must be selected. Also, row i must be all black? Actually for such a black cell, column has white, so row i must be all black (no W) to be selectable; but if row i has a white, then it would be impossible because then both row and column have white. However, the problem guarantees feasibility, so such rows will be all black. But we can still compute forced rows as rows that have at least one black cell in a column that is not all black. Similarly, forced_columns = set of columns j such that there exists i with board[i][j]=='B' and not row_all_black[i].
But we must also consider: Could a row be forced even if it is not all black? That would be impossible because if a row has a white, it cannot be selected. But if there is a black cell in a column with white, the row must be selected; but if the row has a white, then it's impossible. So in a feasible instance, such a row must be all black. So we can still compute forced rows as described; they will be all black.
Similarly forced columns will be all black.
Thus algorithm:
-
Read 8 strings.
-
For each row i (0..7), check if contains 'W'. rowHasW[i] = true if any char is 'W'.
So rowAllB[i] = !rowHasW[i].
-
For each column j, compute colHasW[j] = true if any board[i][j]=='W'. colAllB[j] = !colHasW[j].
-
forcedRows = set(), forcedCols = set().
-
For each cell (i,j) where board[i][j]=='B':
if rowHasW[i] and not colHasW[j]: forcedCols.add(j)
elif not rowHasW[i] and colHasW[j]: forcedRows.add(i)
elif rowHasW[i] and colHasW[j]: impossible (but ignore or assert)
else: # both false, i.e., rowAllB and colAllB; no force
Note: The conditions can be simplified: if rowHasW[i] then forcedCols.add(j) (since then column must be all black). But need to ensure column does not have white? Actually if rowHasW[i] is true, then row can't be selected, so column j must be selected. However, if column j also has white, that would be impossible. But if it's possible, column j must be all black. So we can still add to forcedCols, and later we may detect inconsistency if column has white. But problem guarantees feasible, so column will be all black. So we can just do:
if rowHasW[i]:
forcedCols.add(j)
if colHasW[j]:
forcedRows.add(i)
But careful: If both rowHasW[i] and colHasW[j] are true, then we would add both forcedRows and forcedCols. But that would be impossible because then we would be forced to select both a row that has white and a column that has white? Actually forcedRows adds i, but row i has white, which is not allowed. But such situation shouldn't occur. Since the problem guarantees feasibility, we can assume this case doesn't happen. To be safe, we can just process and if we ever add a forced row that has white or forced column that has white, we could set something, but guarantee avoids it.
However, we must be careful: If a black cell has rowHasW false (i.e., row all black) and colHasW false (col all black), then neither forced. That's fine.
But what about a black cell where rowHasW true and colHasW false: then forcedCols.add(j) (since row has white, need column). Also colHasW false, so column is all black, allowed. For a black cell where rowHasW false and colHasW true: forcedRows.add(i). For a black cell where both false: none. For both true: would add both, but then row has white (cannot be selected) and column has white (cannot be selected), contradiction; so won't happen.
So we can compute forcedRows and forcedCols as:
For each black cell:
if rowHasW[i]: forcedCols.add(j)
if colHasW[j]: forcedRows.add(i)
But note: In case both true, we add both, but then the forced row and column are invalid. Since problem guarantees no such case, it's fine.
However, there is nuance: Suppose a black cell has rowHasW true, we add column j to forcedCols. But if there is another black cell in same row but different column, we will add those columns too. So forcedCols will be set of columns that appear in black cells in rows that have white. That matches.
But we should test with examples:
Example1:
RB = {row2} (since row2 all B). CB = {col4, col7}. forcedRows = {row2} (from black cells in columns with white, e.g., column1 etc). forcedCols = {col4, col7} (from black cells in rows with white). Then R_rem = RB \ forcedRows = empty. C_rem = CB \ forcedCols = empty. min(0,0)=0. Answer = 1+2=3. Good.
Example2:
RB = {row2}. CB = empty. forcedRows = {row2}. forcedCols = empty. R_rem = empty. C_rem = empty. Answer=1+0=1.
Case all white:
rowAllB: all false because each row has W. RB empty. colAllB all false. forcedRows empty, forcedCols empty. R_rem empty, C_rem empty. Answer=0. Good.
Case all black:
All rows all B? Actually all rows have no W, so rowAllB all true. Similarly columns all B, colAllB all true. RB size 8, CB size 8. Now forcedRows? For each black cell, rowHasW false, colHasW false, so no forced rows or columns. So forcedRows empty, forcedCols empty. R_rem = RB = 8, C_rem = CB = 8. min(8,8)=8. Answer = 0+0+8 = 8. Which matches earlier reasoning.
But is it optimal? Could we do with 8 strokes? Yes, either all rows or all columns. Could there be a solution with 7? As argued, no. So 8 is correct.
Now test a more complex case: Suppose we have a board where RB = {1,2}, CB = {3,4}, and no forced rows/columns (i.e., no black cells in rows with white or columns with white). This would mean that all rows not in RB have no black cells? Actually if RB are rows all black, rows not in RB have at least one white. If those rows have any black cells, that would force columns, so forced columns would exist. To have no forced columns, rows with white must have no black cells. So rows with white are all white. Similarly, columns with white must have no black cells. So the board consists of rows all black (RB) and columns all black (CB) and the rest (rows with white and columns with white) are white? But rows with white are all white, and columns with white are all white? Actually if a row has white, it might still have some black cells only if forced columns cover them. To have no forced columns, rows with white must have no black cells, meaning they are all white. Similarly, columns with white must have no black cells, meaning they are all white. So the board would have black only in the intersection of RB and CB. And RB rows are all black across all columns, but if a column has white, then that column has at least one white somewhere; but if RB rows are all black, then for those rows, even columns with white would be black (since row all black). That would create black cells in columns with white, which would force rows? Wait: If column j has white (i.e., there exists some row i' where board[i'][j] = W), but if j is not in CB (i.e., column has a white), then for any row i in RB, (i,j) is black (since row i all black). That would be a black cell in a column with white, which would force row i to be selected. That would make forced rows. So to avoid forced rows, we cannot have any black cell in a column with white. That means for any column j not in CB (i.e., has a white), it must have no black cells. But rows in RB are all black, so they would produce black cells in those columns. So that forces that if there is any row in RB, then every column must be all black (i.e., CB must be all columns) to avoid forcing rows. Similarly, if there is any column in CB, then every row must be all black to avoid forcing columns. So the only way to have both RB nonempty and CB nonempty without forced rows/columns is if RB = all rows? Or CB = all columns? Let's analyze: RB nonempty means there is at least one all-black row. For any column j that has a white (i.e., not all black), that column would have a black cell at the all-black row (since that row is all black). That black cell is in a column with white, so it would force the row (the all-black row) to be selected. That would add forced row. But forced rows are allowed; they would be selected. So it's possible to have RB and CB both nonempty and forced rows/columns present. The forced rows would be some of the RB rows, and forced columns would be some of the CB columns. So in general, forced rows and columns can be nonempty.
But our formula R_rem = RB \ forcedRows, C_rem = CB \ forcedCols, and answer = |forcedRows|