Escape a Large Maze Python

In this problem, we need to determine if it's possible to navigate from a source position to a target position in a massive 1 million × 1 million grid while avoiding blocked cells. The key insight is that we can't perform a full BFS/DFS on such a large grid, so we use a clever optimization.

Problem Analysis

The main challenge is the grid size − searching 1 million × 1 million cells would be too slow. However, if there are only a small number of blocked cells, they can only create a limited "enclosed area". The maximum area that can be enclosed by n blocked cells is approximately n*(n-1)/2, which for typical constraints (n ? 200) gives us around 20,000 cells.

Algorithm Approach

We use DFS with an optimization − if we can explore more than 20,000 cells from a starting point, we know we're not trapped in a small enclosed area. We need to check reachability from both source to target AND target to source to ensure both points aren't trapped separately.

Implementation

class Solution:
    def isEscapePossible(self, blocked, source, target):
        blocked = set(map(tuple, blocked))
        
        def dfs(x, y, target, seen):
            # Check bounds, blocked cells, and already visited cells
            if not (0 <= x < 10**6 and 0 <= y < 10**6) or (x, y) in blocked or (x, y) in seen:
                return False
            
            seen.add((x, y))
            
            # If we've explored enough cells or reached target, we're not trapped
            if len(seen) > 20000 or [x, y] == target:
                return True
            
            # Explore all four directions
            return (dfs(x + 1, y, target, seen) or
                    dfs(x - 1, y, target, seen) or
                    dfs(x, y + 1, target, seen) or
                    dfs(x, y - 1, target, seen))
        
        # Check reachability from both directions
        return (dfs(source[0], source[1], target, set()) and
                dfs(target[0], target[1], source, set()))

# Test the solution
solution = Solution()
result = solution.isEscapePossible([[0,1],[1,0]], [0,0], [0,3])
print(f"Can escape: {result}")
Can escape: False

How It Works

The algorithm works by:

  • Bidirectional Check: We verify reachability from source to target AND target to source

  • Escape Threshold: If we can visit more than 20,000 cells, we assume we're not trapped

  • Early Termination: Stop searching once we reach the target or exceed the threshold

  • Boundary Conditions: Handle grid boundaries and blocked cells properly

Example Walkthrough

In the given example with blocked = [[0,1],[1,0]], source = [0,0], target = [0,3]:

S T (0,0) (0,3) Blocked Blocked

The source at (0,0) is blocked from reaching the target at (0,3) because the blocked cells at (0,1) and (1,0) create a barrier.

Time Complexity

The time complexity is O(min(20000, total_reachable_cells)) for each DFS call, making it much more efficient than exploring the entire 1M × 1M grid.

Conclusion

This solution efficiently handles the large maze escape problem by recognizing that blocked cells can only create limited enclosed areas. The bidirectional search with a 20,000 cell threshold ensures we can determine reachability without exploring the entire massive grid.

Updated on: 2026-03-25T08:42:33+05:30

511 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements