Ctrl K

Block the Only Route

Medium
A warehouse is an m × n table grid: 1 is a free cell, 0 is blocked. A forklift starts at cell (0, 0) and has to reach (m - 1, n - 1); it can only move down or right, and only through free cells. You may flip the value of at most one cell (1 to 0 or 0 to 1), except the start and the finish. Changing nothing is also allowed. Return True if this way you can make the finish unreachable for the forklift, and False otherwise.
Example 1
grid=[[1, 1, 0], [0, 1, 1], [1, 1, 1]] → True

Every route passes through cell (0, 1): from the corner the only move is to the right, since below is a 0. Block it and no route is left.

Example 2
grid=[[1, 1, 1], [1, 1, 1]] → False

Every inner cell can be bypassed by another route, so one blocked cell is not enough.

Example 3
grid=[[1, 0], [0, 1]] → True

There is no route to begin with, so nothing has to change.

Constraints
  • 1 ≤ m, n ≤ 1000
  • m · n ≤ 10⁵
  • all rows have the same length
  • grid[i][j] is 0 or 1
  • grid[0][0] = grid[m - 1][n - 1] = 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Uphill Walk HardUphill-Only Walks HardDrain the Landlocked Ponds MediumLand Regions on a Map MediumRain Reaching Both Seas Medium
loading Python…
Input
grid=[[1, 1, 0], [0, 1, 1], [1, 1, 1]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.