Ctrl K

Escape the Huge Grid

Hard
The field is a square of 10⁶ × 10⁶ cells; a cell is given by its coordinates [x, y] with 0 ≤ x, y < 10⁶. Some cells hold walls, listed in walls. A robot stands on cell start and wants to get to cell finish. In one move it steps to a cell sharing a side with its current one, provided that cell is not a wall and is still inside the field. Return True if the robot can reach finish, and False otherwise. The field is far too large to explore in full: think about how many cells a mere 200 walls can possibly shut in.
Example 1
walls=[[1, 0], [1, 1], [0, 2]], start=[0, 0], finish=[5, 5] → False

The walls (1, 0), (1, 1), (0, 2) together with the edge of the field lock in the cells (0, 0) and (0, 1), so there is no way out of that corner.

Example 2
walls=[[3, 3], [3, 4], [4, 3]], start=[2, 2], finish=[4, 4] → True

The walls cover finish from two sides only: you can reach (4, 4) through (5, 4) or (4, 5).

Example 3
walls=[], start=[999999, 0], finish=[0, 999999] → True

There are no walls, so every cell can be reached.

Constraints
  • 0 ≤ len(walls) ≤ 200
  • walls[i] = [x, y], 0 ≤ x, y < 10⁶
  • start and finish are pairs [x, y], 0 ≤ x, y < 10⁶
  • start ≠ finish
  • Neither start nor finish is a wall
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsOne-Swap Word Families HardWhich Machine to Cure HardWhich Machine to Unplug HardStop a Process Branch MediumMerge Contact Cards Medium
loading Python…
Input
walls=[[1, 0], [1, 1], [0, 2]], start=[0, 0], finish=[5, 5]
Expected
False

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