Deleting Boundaries (APCS 2019-10 Expert)
2.0s 256MYou are given a binary rectangle. In one operation, choose its current top row, bottom row, leftmost column, or rightmost column. Flip any cells needed to make that entire border uniformly \(0\) or uniformly \(1\), then remove it. Flipping a cell costs \(1\); removal itself is free. Find the minimum total number of flips needed to remove the whole rectangle.
Input Format
The first line contains the row and column counts \(m,n\). The next \(m\) lines each contain \(n\) binary values.
Output Format
Print the minimum total number of flips.
Constraints
\(1\le m,n\le25\).
Sample Input 1
4 5
0 1 0 1 1
1 1 1 0 1
0 0 0 0 0
0 0 0 1 0
Sample Output 1
2
Sample Input 2
3 5
0 0 0 1 0
1 0 1 1 1
0 0 0 1 0
Sample Output 2
1
Source
APCS 2019-10 public archive version: tcirc:d082。
Log in to write and submit code.
Log in