Escape a Large Maze Problem
Description
LeetCode Problem 1036.
There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are (x, y).
We start at the source = [s_x, s_y] square and want to reach the target = [t_x, t_y] square. There is also an array of blocked squares, where each blocked[i] = [x_i, y_i] represents a blocked square with coordinates (x_i, y_i).
Each move, we can walk one square north, east, south, or west if the square is not in the array of blocked squares. We are also not allowed to walk outside of the grid.
Return true if and only if it is possible to reach the target square from the source square through a sequence of valid moves.
Example 1:
Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
Output: false
Explanation: The target square is inaccessible starting from the source square because we cannot move.
We cannot move north or east because those squares are blocked.
We cannot move south or west because we cannot go outside of the grid.
Example 2:
Input: blocked = [], source = [0,0], target = [999999,999999]
Output: true
Explanation: Because there are no blocked cells, it is possible to reach the target square.
Constraints:
0 <= blocked.length <= 200blocked[i].length == 20 <= x_i, y_i < 10^6source.length == target.length == 20 <= s_x, s_y, t_x, t_y < 10^6source != target- It is guaranteed that
sourceandtargetare not blocked.
Solution
Approach: Bounded BFS From Both Ends
The grid has 1012 squares, so a plain search is out of the question. But there are at most 200 blocked squares, and that few squares can only fence in a small area.
The largest area k blocked squares can enclose is a triangle cut off at a corner of the grid: a diagonal wall of k squares closes off 1 + 2 + ... + (k - 1) = k * (k - 1) / 2 squares. For k = 200 that is 19,900 squares. (A wall away from the corners has to close on itself, which encloses even less.)
So run a BFS from source:
- If it reaches
target, we are done. - If it visits more than
k * (k - 1) / 2squares, no wall can be holding it in:sourceis in the open part of the grid. - If the BFS runs out of squares first,
sourceis trapped andtargetis not with it.
Escaping from source alone is not enough, because target itself may be the one that is fenced in (for example, sitting in a corner behind two blocked squares). So run the same search from target too, and answer true only if both searches succeed. Two points that are both in the open part of the grid can always reach each other, since 200 squares cannot cut the grid in two.
Complexity: O(k2) time and space, where k is the number of blocked squares: each BFS stops after about k2 / 2 squares.
import collections
class Solution:
def isEscapePossible(self, blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
walls = {(x, y) for x, y in blocked}
# k blocked squares can wall off at most k * (k - 1) / 2 squares (a diagonal across a corner).
limit = len(blocked) * (len(blocked) - 1) // 2
size = 10 ** 6
def escapes(start: List[int], goal: List[int]) -> bool:
# BFS from start: done once we reach goal or see more squares than any wall can hold.
seen = {(start[0], start[1])}
queue = collections.deque(seen)
while queue:
x, y = queue.popleft()
for nx, ny in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
if 0 <= nx < size and 0 <= ny < size and (nx, ny) not in walls and (nx, ny) not in seen:
if nx == goal[0] and ny == goal[1]:
return True
seen.add((nx, ny))
if len(seen) > limit:
return True
queue.append((nx, ny))
return False
return escapes(source, target) and escapes(target, source)