題目描述:
給定一個 n x n 的二元矩陣 grid,其中恰好有兩個島嶼(由 1 組成的連通區域)。你可以將 0 變為 1,求連接兩個島嶼所需翻轉的最少 0 的數量(即兩島之間的最短距離)。
解題思路: DFS 標記 + BFS 擴展:
1,用 DFS 標記整個島的所有格子(標記為 2),同時將邊界格子加入 BFS 佇列。1 的格子(第二個島),此時的層數即為答案。時間複雜度:O(n^2) — DFS 和 BFS 各自最多遍歷整個矩陣一次。
空間複雜度:O(n^2) — BFS 佇列和 DFS 遞迴棧在最差情況下可能包含 O(n^2) 個元素。
1. Find any cell with value 1 (part of first island). 2. DFS from that cell: a. Mark visited cells as 2. b. Add all cells of the first island to BFS queue. 3. BFS level-by-level from the first island: a. For each cell in current level, explore 4 neighbors. b. If neighbor is 1 (second island), return current step count. c. If neighbor is 0, mark as 2, add to queue. d. Increment step count after each level. 4. Return step count when second island is reached.
雙向 BFS:從兩個島同時開始 BFS 擴展,當兩個搜索前沿相遇時即為答案。可以將搜索空間減半,但實作較複雜。
Union-Find + 排序:將所有水域格子按到兩島的距離排序,逐步合併直到兩島連通。時間 O(n^2 log n),不如 BFS 直接。