Disconnect Path in a Binary Matrix by at Most One Flip - Problem

You're given an m × n binary matrix representing a grid where you can only move through cells containing 1. Your goal is to determine if you can disconnect the path from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1) by flipping at most one cell.

Movement Rules:

  • From cell (row, col), you can move to (row + 1, col) or (row, col + 1)
  • You can only move to cells with value 1

Flipping Rules:

  • You can flip at most one cell (changing 0→1 or 1→0)
  • You cannot flip the start (0, 0) or end (m-1, n-1) cells

Return true if you can make the matrix disconnected, false otherwise.

Input & Output

example_1.py — Basic disconnectable grid
$ Input: grid = [[1,1,1],[1,0,0],[1,1,1]]
Output: true
💡 Note: We can flip grid[1][0] from 1 to 0, making it impossible to reach from (0,0) to (2,2). The path through (1,0) is the only way to connect the regions.
example_2.py — Multiple paths available
$ Input: grid = [[1,1,1],[1,1,0],[1,1,1]]
Output: false
💡 Note: There are multiple independent paths from (0,0) to (2,2). Even if we flip one cell, alternative routes still exist, so we cannot disconnect the grid with just one flip.
example_3.py — Already disconnected
$ Input: grid = [[1,0,0],[1,1,0],[0,1,1]]
Output: true
💡 Note: The grid is already disconnected - there's no path from (0,0) to (2,2). Since we can achieve disconnection without any flips, the answer is true.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 50
  • grid[i][j] is either 0 or 1
  • grid[0][0] == grid[m - 1][n - 1] == 1

Visualization

Tap to expand
Disconnect Path in Binary Matrix INPUT Binary Matrix (3x3) 1 START 1 1 1 0 0 1 1 1 END = 1 (passable) = 0 (blocked) Move: right or down only Flip: at most 1 cell (except start/end) ALGORITHM STEPS 1 Find First Path DFS from (0,0) to (m-1,n-1) Mark visited cells 2 Remove Path Cells Set visited cells to 0 (simulates one flip) 3 Find Second Path Try DFS again on modified grid 4 Check Result No 2nd path = true Has 2nd path = false Path Analysis: Path 1: top After removal: No path 2! Disconnected FINAL RESULT Can disconnect path? true Why true? 1. Only one path exists: (0,0)-->(0,1)-->(0,2) -->(1,0)-->(2,0)-->(2,1) -->(2,2) 2. Flip any path cell (except start/end) 3. Grid becomes disconnected! Output: return true Key Insight: If only one unique path exists from start to end, flipping ANY cell on that path will disconnect the grid. The optimal approach runs DFS twice: first to find and "remove" a path, then to check if another path exists. Time Complexity: O(m*n) | Space Complexity: O(m*n) for recursion stack TutorialsPoint - Disconnect Path in a Binary Matrix by at Most One Flip | Optimal Solution
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
31.3K Views
Medium Frequency
~25 min Avg. Time
876 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen