LeetCode 1036. Escape a Large Maze Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1036. Escape a Large Maze

Description

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.

Solutions

Solution 1: DFS

The problem can be interpreted as determining whether it is possible to move from a source point to a target point in a 106 × 106 grid, given a small number of blocked points.

Since the number of blocked points is small, the maximum area that can be blocked is no more than |blocked|^2 / 2. Therefore, we can perform a depth-first search (DFS) starting from both the source and the target points. The search continues until either the target point is reached or the number of visited points exceeds |blocked|^2 / 2. If either condition is satisfied, return true. Otherwise, return false.

Time complexity is O(m), and space complexity is O(m), where m is the size of the blocked region. In this problem, m ≤ |blocked|^2 / 2 = 2002 / 2 = 20000.

PythonJavaC++GoTypeScriptRust
class Solution: def isEscapePossible( self, blocked: List[List[int]], source: List[int], target: List[int] ) -> bool: def dfs(source: List[int], target: List[int], vis: set) -> bool: vis.add(tuple(source)) if len(vis) > m: return True for a, b in pairwise(dirs): x, y = source[0] + a, source[1] + b if 0 <= x < n and 0 <= y < n and (x, y) not in s and (x, y) not in vis: if [x, y] == target or dfs([x, y], target, vis): return True return False s = {(x, y) for x, y in blocked} dirs = (-1, 0, 1, 0, -1) n = 10**6 m = len(blocked) ** 2 // 2 return dfs(source, target, set()) and dfs(target, source, set())(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !