## Algorithm

Problem Name: 1036. Escape a Large Maze

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]` represents a blocked square with coordinates `(xi, yi)`.

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 <= 200`
• `blocked[i].length == 2`
• `0 <= xi, yi < 106`
• `source.length == target.length == 2`
• `0 <= sx, sy, tx, ty < 106`
• `source != target`
• It is guaranteed that `source` and `target` are not blocked.

## Code Examples

### #1 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const isEscapePossible = function(blocked, source, target) {
const blockedSet = new Set()
for (let el of blocked) {
let key = el[0] + "," + el[1]
}
return canVisit(blockedSet, source, target) && canVisit(blockedSet, target, source)
}

function canVisit(blocked, start, end) {
const visited = new Set()
return dfs(blocked, start[0], start[1], end[0], end[1], visited)
}
function dfs(blocked, i, j, m, n, visited) {
const dirs = [[i - 1, j], [i + 1, j], [i, j + 1], [i, j - 1]]
if ((i == m && j == n) || visited.size >= 20000) {
return true
}
for (let dir of dirs) {
let nextKey = dir[0] + "," + dir[1]
if (
dir[0] >= 0 &&
dir[1] >= 0 &&
dir[0] < 1e6 &&
dir[1] < 1e6 &&
!blocked.has(nextKey) &&
!visited.has(nextKey)
) {
if (dfs(blocked, dir[0], dir[1], m, n, visited)) {
return true
}
}
}
return false
}
``````
Copy The Code &

Input

cmd
blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]

Output

cmd
false

### #2 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def isEscapePossible(self, blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
if not blocked: return True
blocked = set(map(tuple, blocked))

def check(blocked, source, target):
si, sj = source
ti, tj = target
level = 0
q = collections.deque([(si,sj)])
vis = set()
while q:
for _ in range(len(q)):
i,j = q.popleft()
if i == ti and j == tj: return True
for x,y in ((i+1,j),(i-1,j),(i,j+1),(i,j-1)):
if 0<=x<10**6 and 0<=y<10**6 and (x,y) not in vis and (x,y) not in blocked:
q.append((x,y))
level += 1
if level == len(blocked): break
else:
return False
return True

return check(blocked, source, target) and check(blocked, target, source)
``````
Copy The Code &

Input

cmd
blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]

Output

cmd
false