LeetCode 题解工作台

逃离大迷宫

在一个 10 6 x 10 6 的网格中,每个网格上方格的坐标为 (x, y) 。 现在从源方格 source = [s x , s y ] 开始出发,意图赶往目标方格 target = [t x , t y ] 。数组 blocked 是封锁的方格列表,其中每个 blocked[i] = [x i…

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

困难 · 数组·哈希·扫描

bolt

答案摘要

题目相当于在一个 $10^6 \times 10^6$ 的网格中,给定源点和目标点,以及一小部分被封锁的点,问是否可以从源点到达目标点。 由于被封锁的点数量很少,最终能封锁的区域大小不超过 $|blocked|^2 / 2$,因此,我们可以从源点和目标点出发,进行深度优先搜索,直到搜索到目标点或者搜索到的点数超过 $|blocked|^2 / 2$,如果都满足,则返回 。否则返回 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

在一个 106 x 106 的网格中,每个网格上方格的坐标为 (x, y)

现在从源方格 source = [sx, sy] 开始出发,意图赶往目标方格 target = [tx, ty] 。数组 blocked 是封锁的方格列表,其中每个 blocked[i] = [xi, yi] 表示坐标为 (xi, yi) 的方格是禁止通行的。

每次移动,都可以走到网格中在四个方向上相邻的方格,只要该方格 在给出的封锁列表 blocked 上。同时,不允许走出网格。

只有在可以通过一系列的移动从源方格 source 到达目标方格 target 时才返回 true。否则,返回 false

 

示例 1:

输入:blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
输出:false
解释:
从源方格无法到达目标方格,因为我们无法在网格中移动。
无法向北或者向东移动是因为方格禁止通行。
无法向南或者向西移动是因为不能走出网格。

示例 2:

输入:blocked = [], source = [0,0], target = [999999,999999]
输出:true
解释:
因为没有方格被封锁,所以一定可以到达目标方格。

 

提示:

  • 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
  • 题目数据保证 sourcetarget 不在封锁列表内
lightbulb

解题思路

方法一:DFS

题目相当于在一个 106×10610^6 \times 10^6 的网格中,给定源点和目标点,以及一小部分被封锁的点,问是否可以从源点到达目标点。

由于被封锁的点数量很少,最终能封锁的区域大小不超过 blocked2/2|blocked|^2 / 2,因此,我们可以从源点和目标点出发,进行深度优先搜索,直到搜索到目标点或者搜索到的点数超过 blocked2/2|blocked|^2 / 2,如果都满足,则返回 true\textit{true}。否则返回 false\textit{false}

时间复杂度 O(m)O(m),空间复杂度 O(m)O(m),其中 mm 是被封锁的区域的大小,本题中 mblocked2/2=2002/2=20000m \leq |blocked|^2 / 2 = 200^2 / 2 = 20000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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())
speed

复杂度分析

指标
时间and space complexity depend on the approach used. BFS typically has O(V + E) complexity where V is the number of squares and E is the number of edges (possible moves). Space complexity is proportional to the number of visited squares, which may also be O(V). With hash set optimizations, the time complexity for blocked square lookups can be reduced to O(1).
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate effectively handles blocked squares with hash lookups.

  • question_mark

    The candidate avoids revisiting squares and correctly uses BFS/DFS to explore the grid.

  • question_mark

    The candidate efficiently manages edge cases, such as blocked loops around the source or target.

warning

常见陷阱

外企场景
  • error

    Failing to account for the case where a path is blocked by surrounding obstacles, causing unnecessary loops.

  • error

    Inefficient space or time usage, especially when the grid is large, leading to excessive memory consumption or slow performance.

  • error

    Incorrectly assuming the target is unreachable due to insufficient exploration or misunderstanding of the BFS/DFS traversal method.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Implementing an optimized DFS that only explores viable paths to avoid excessive backtracking.

  • arrow_right_alt

    Using BFS with a priority queue to prioritize movement in certain directions.

  • arrow_right_alt

    Solving the problem by implementing a dynamic programming approach that considers each possible move.

help

常见问题

外企场景

逃离大迷宫题解:数组·哈希·扫描 | LeetCode #1036 困难