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
Visualization
Time & Space Complexity
Where B is the number of blocked cells (at most 200). We explore at most Bยฒ cells from each of source and target
Visited set stores at most Bยฒ coordinates for each BFS
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