You are given a binary matrix where each cell contains either 0 or 1. Your mission is to make the matrix "well-isolated" by strategically flipping 1s to 0s.
A matrix is considered well-isolated when no 1 has any neighboring 1s in the four cardinal directions (up, down, left, right). Think of it like ensuring no two islands touch each other!
Goal: Find the minimum number of flip operations needed to achieve this well-isolated state.
Example: In matrix [[1,1,0],[0,1,1],[0,0,1]], you need to flip at least 2 ones to make it well-isolated.
Input & Output
Time & Space Complexity
O(m*n) to scan matrix and find components, O(k^2.5) for maximum matching where k is number of 1s
Space for graph representation and matching algorithm data structures
Constraints
- m == grid.length
- n == grid[i].length
- 1 β€ m, n β€ 15
- grid[i][j] is either 0 or 1
- The number of 1s in the grid is at most 20