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 = [sx, sy] square and want to reach the target = [tx, ty] square. There is also an array of blocked squares, where each blocked[i] = [xi, yi] is a blocked square with coordinates (xi, yi).
Each move, we can walk one square north, east, south or west if that square is not in the blocked array. 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.
Function Signature
def is_escape_possible(blocked: list[list[int]], source: list[int], target: list[int]) -> bool:
Rules
-
A square is inside the grid when both of its coordinates are between 0 and 999,999 inclusive.
-
From
(x, y)
the four possible moves go to
(x + 1, y)
,
(x - 1, y)
,
(x, y + 1)
and
(x, y - 1)
.
Constraints
-
0 <= blocked.length <= 200
-
blocked[i].length == 2
-
0 <= xi, yi < 10^6
-
source.length == target.length == 2
-
0 <= sx, sy, tx, ty < 10^6
-
source != target
-
It is guaranteed that
source
and
target
are not blocked.
-
All
blocked
cells are distinct.
Examples
Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
Output: False
The source is boxed into the corner. Its only neighbors inside the grid are (1,0) and (0,1), and both are blocked.
Input: blocked = [], source = [0,0], target = [999999,999999]
Output: True
There are no blocked cells, so the target can be reached.
Input: blocked = [[0,2],[1,1],[2,0]], source = [5,5], target = [0,0]
Output: False
The three blocked cells form a diagonal wall that closes off the corner pocket (0,0), (1,0), (0,1). The target is inside that pocket and the source is outside it, so no path exists.