算法--最短的桥

23 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情

题目

leetcode 934. 最短的桥 难度:中等

给你一个大小为 n x n 的二元矩阵 grid ,其中 1 表示陆地,0 表示水域。

岛 是由四面相连的 1 形成的一个最大组,即不会与非组内的任何其他 1 相连。grid 中 恰好存在两座岛 。

你可以将任意数量的 0 变为 1 ,以使两座岛连接起来,变成 一座岛 。

返回必须翻转的 0 的最小数目。

示例 1:

输入:grid = [[0,1],[1,0]]
输出:1

示例 2:

输入:grid = [[0,1,0],[0,0,0],[0,0,1]]
输出:2

示例 3:

输入:grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
输出:1
 

提示:

  • n == grid.length == grid[i].length
  • 2 <= n <= 100
  • grid[i][j] 为 0 或 1
  • grid 中恰有两个岛

题解

题目中求最少的翻转 0 的数目等价于求矩阵中两个岛的最短距离,因此我们可以广度优先搜索来找到矩阵中两个块的最短距离。首先找到其中一座岛,然后将其不断向外延伸一圈,直到到达了另一座岛,延伸的圈数即为最短距离。广度优先搜索时,我们可以将已经遍历过的位置标记为−1,实际计算过程如下:

我们通过遍历找到数组 grid 中的 1 后进行广度优先搜索,此时可以得到第一座岛的位置集合,记为 island,并将其位置全部标记为 −1。 随后我们从island 中的所有位置开始进行广度优先搜索,当它们到达了任意的 1 时,即表示搜索到了第二个岛,搜索的层数就是答案。

/**
 * @param {number[][]} grid
 * @return {number}
 */
var shortestBridge = function(grid) {
    const n = grid.length;
    const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]];
    const island = [];
    const queue = [];

    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] === 1) {
                queue.push([i, j]);
                grid[i][j] = -1;
                while (queue.length !== 0) {
                    const cell = queue.shift();
                    let x = cell[0], y = cell[1];
                    island.push(cell);
                    for (let k = 0; k < 4; k++) {
                        let nx = x + dirs[k][0];
                        let ny = y + dirs[k][1];
                        if (nx >= 0 && ny >= 0 && nx < n && ny < n && grid[nx][ny] == 1) {
                            queue.push([nx, ny]);
                            grid[nx][ny] = -1;
                        }
                    }
                }
                for (const cell of island) {
                    queue.push(cell);
                }
                let step = 0;
                while (queue.length !== 0) {
                    const sz = queue.length;
                    for (let k = 0; k < sz; k++) {
                        const cell = queue.shift();
                        let x = cell[0], y = cell[1];
                        for (let d = 0; d < 4; d++) {
                            let nx = x + dirs[d][0];
                            let ny = y + dirs[d][1];
                            if (nx >= 0 && ny >= 0 && nx < n && ny < n) {
                                if (grid[nx][ny] === 0) {
                                    queue.push([nx, ny]);
                                    grid[nx][ny] = -1;
                                } else if (grid[nx][ny] === 1) {
                                    return step;
                                }
                            }
                        }
                    }
                    step++;
                }
            }
        }
    }
    return 0;
};

代码详解

  1. ans = 0
  2. 找到第一个 陆地点
  3. 根据这个点 进行 dfs 标记所有的岛屿点
  4. 将搜索过的陆地节点标记为 -1

与此同时 将所有相邻的海洋节点存入队列

  1. 该海洋节点标记为 2

搜索所有的海洋队列 bfs,ans += 1

5.1 该海洋节点标记为 -1

5.2 海洋节点 加入下轮搜索队列

5.3 陆地节点 返回结果

6. 返回 ans