Problem Description
Given an m x n binary matrix grid, you can only move either down or right from a cell (row, col) and only if the destination cell has value 1. The grid is considered disconnected if there is no valid path from the top-left cell (0, 0) to the bottom-right cell (m-1, n-1). You are allowed to flip at most one cell (change a 1 to 0 or vice versa), except for the start and the end cells. Return true if it is possible to disconnect the grid by at most one flip; otherwise, return false.
Key Insights
- First, check if a valid path exists; if the matrix is already disconnected, return true.
- If a path exists, compute one valid path from start to finish using DFS/BFS.
- The idea is to try flipping a cell on this path (except the endpoints) and then re-check connectivity.
- Usually, flipping the second cell in the found path is a promising candidate because if multiple alternative paths existed, that cell would not be critical.
- After “removing” (or flipping) that candidate cell (simulate by marking it as 0), re-run the search to see if the path is broken.
Space and Time Complexity
Time Complexity: O(mn) – We traverse the grid several times (once to get the initial path and once more to check connectivity after a flip). Space Complexity: O(mn) – The recursion/queue storage may hold up to all cells in worst case.
Solution
We start by searching for an initial valid path from (0, 0) to (m - 1, n - 1) using DFS (or BFS). If no path exists initially, the grid is already disconnected and we return true.
If a path is found, we choose a candidate cell (typically the second cell in the path, ensuring it is not the start or end) as the cell to flip. The intuition is that if this candidate cell is “critical” (i.e. every valid path must pass through it), flipping it will disconnect the grid. We then simulate flipping the candidate cell by marking it as 0 and run another DFS/BFS from (0, 0) to (m - 1, n - 1). If no new path exists, then flipping that cell is sufficient to disconnect the grid and we return true. Otherwise, if an alternative path exists, it means no single flip can disconnect the grid and we return false.
Important details:
- Always ensure that the endpoints (start and finish) are not flipped.
- The search is limited to moves down and right, so the candidate cell’s removal might cut off all possible paths if it is unique to them.
- Corner cases such as very short paths (e.g. grid of size 1 x n or m x 1) should be handled carefully.