Coin Flipping 2
Time limit1sMemory limit128 MB
Given an N x N grid of H/T coins, find the minimum number of tails achievable by flipping any subset of rows and columns.
- Level
Medium6 of 10
- Topics
- Brute force, Bit manipulation, Greedy
- Solved
- No attempts yet
Problem
There are N^2 coins on a table, arranged in N rows and N columns. Each coin shows either heads (H) or tails (T). The following figure shows one possible arrangement when N = 3.

In one operation, you may choose any one row or any one column and flip all N coins in it. For example, if the first column of Figure 1 is flipped, the board becomes Figure 2. If the first row of Figure 2 is then flipped, the board becomes Figure 3.
In Figure 3, exactly two coins show tails. Starting from Figure 1, it is impossible to make the number of tail-up coins smaller than two by repeatedly applying the allowed operation.
Given the initial state of the N^2 coins, find the minimum possible number of coins that can show tails after any sequence of row and column flips.
Input
The first line contains a natural number N not greater than 32.
Each of the next N lines contains a string of length N, describing one row from left to right. H means the coin is heads up, and T means the coin is tails up. There are no spaces inside a row.
Output
Print the minimum possible number of tail-up coins after performing any sequence of operations, where each operation flips all N coins in one row or one column.

