Imagine you're trapped in a massive 1 million ร— 1 million grid and need to escape! You start at a source position [sx, sy] and must reach a target position [tx, ty].

However, there's a catch: certain squares are blocked and you cannot pass through them. You can only move in four directions: north, south, east, or west - one square at a time.

The Challenge: Given an array of blocked coordinates, determine if it's possible to reach the target from the source. The twist? The grid is so large that a naive BFS/DFS would timeout, so you need a clever insight!

Key Insight: With at most 200 blocked squares, the maximum area that can be enclosed is limited. If we can explore more than this maximum area from either source or target, we know we're not trapped!

Input & Output

example_1.py โ€” Basic escape possible
$ Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
โ€บ Output: false
๐Ÿ’ก Note: The source is blocked from reaching the target by the blocked squares. From [0,0] we can only move to unblocked adjacent squares, but the blocked squares at [0,1] and [1,0] prevent any path to [0,2].
example_2.py โ€” Empty blocked array
$ Input: blocked = [], source = [0,0], target = [999999,999999]
โ€บ Output: true
๐Ÿ’ก Note: With no blocked squares, we can always find a path from source to target in the grid. The path exists even though it might be very long.
example_3.py โ€” Large enclosed area
$ Input: blocked = [[0,0],[0,1],[0,2],[1,0],[2,0]], source = [1,1], target = [999999,999999]
โ€บ Output: false
๐Ÿ’ก Note: The source [1,1] is completely enclosed by the blocked squares forming a barrier. No matter which direction we move from [1,1], we hit a blocked square or the boundary formed by blocked squares.

Visualization

Tap to expand
Maximum Enclosed Area with N Blocked CellsWorst Case: Triangle FormationTrapped AreaMax Area = Nร—(N-1)/2Example: 7 blocks โ†’ 21 cells maxBFS Exploration StrategyStartIf explored > max_areaโ†’ Escape possible!If explored โ‰ค max_areaโ†’ Might be trapped๐Ÿ” Algorithm Steps:1. Calculate max_area = blocked_count ร— (blocked_count - 1) / 22. BFS from source: if explored > max_area OR reached target โ†’ source_free = true3. BFS from target: if explored > max_area OR reached source โ†’ target_free = true
Understanding the Visualization
1
Optimal Barrier Placement
The worst case is when blocked cells form a triangle against the grid boundary
2
Calculate Max Area
A triangle with B blocked cells can enclose at most B*(B-1)/2 area
3
Early Escape Detection
If BFS explores more than this area, we've proven escape is possible
4
Bidirectional Check
Test both source and target to ensure neither is trapped
Key Takeaway
๐ŸŽฏ Key Insight: The maximum area that N blocked cells can enclose is Nร—(N-1)/2. If BFS explores more cells than this from either source or target, that point cannot be trapped!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(Bยฒ)

Where B is the number of blocked cells (at most 200). We explore at most Bยฒ cells from each of source and target

n
2n
โœ“ Linear Growth
Space Complexity
O(Bยฒ)

Visited set stores at most Bยฒ coordinates for each BFS

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค blocked.length โ‰ค 200
  • blocked[i].length == 2
  • 0 โ‰ค xi, yi < 106
  • source.length == target.length == 2
  • 0 โ‰ค sx, sy, tx, ty < 106
  • source โ‰  target
  • All blocked squares are unique
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 22
26.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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