LeetCode 1036: Escape a Large Maze Solution

Master LeetCode problem 1036 (Escape a Large Maze), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we can use BFS (Breadth-First Search) algorithm. We need to keep track of the blocked squares and explore all possible valid moves from the source square. We stop exploring when we reach the target square or when we have visited all reachable squares.

  1. We start by creating a HashSet to store the blocked squares for quick lookup.
  2. We initialize a queue for BFS and add the source square to it.
  3. We explore all possible valid moves from the current square, checking if the move is within the grid bounds and not blocked.
  4. We continue exploring until we reach the target square, or until we have visited all reachable squares.
  5. If we reach the target square, we return true. Otherwise, we return false.

Time Complexity: O(min(N, B)) where N is the number of blocked squares and B is the maximum number of squares we need to explore (in the worst case, the entire grid).

Space Complexity: O(N) where N is the number of blocked squares.

Solution Code

import java.util.*;

class Solution {
    public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
        Set<String> blockedSet = new HashSet<>();
        for (int[] block : blocked) {
            blockedSet.add(block[0] + "," + block[1]);
        }
        
        return isEscapePossibleHelper(blockedSet, source, target) && isEscapePossibleHelper(blockedSet, target, source);
    }
    
    public boolean isEscapePossibleHelper(Set<String> blockedSet, int[] source, int[] target) {
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int limit = 2000; // Limit the number of squares to explore
        
        Queue<int[]> queue = new LinkedList<>();
        Set<String> visited = new HashSet<>();
        
        queue.offer(source);
        visited.add(source[0] + "," + source[1]);
        
        while (!queue.isEmpty() && limit-- > 0) {
            int[] current = queue.poll();
            if (current[0] == target[0] && current[1] == target[1]) {
                return true;
            }
            for (int[] dir : directions) {
                int nextX = current[0] + dir[0];
                int nextY = current[1] + dir[1];
                String key = nextX + "," + nextY;
                if (nextX >= 0 && nextX < 1000000 && nextY >= 0 && nextY < 1000000 && !blockedSet.contains(key) && !visited.contains(key)) {
                    queue.offer(new int[]{nextX, nextY});
                    visited.add(key);
                }
            }
        }
        
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1036 (Escape a Large Maze)?

This page provides optimized solutions for LeetCode problem 1036 (Escape a Large Maze) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1036 (Escape a Large Maze)?

The time complexity for LeetCode 1036 (Escape a Large Maze) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1036 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1036 in Java, C++, or Python.

Back to LeetCode Solutions