ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

leetcode 934. Shortest Bridge 最短的桥(中等)

2022-06-07 23:32:49  阅读:215  来源: 互联网

标签:Bridge tx ty int queue length grid leetcode Shortest


一、题目大意

标签: 搜索

https://leetcode.cn/problems/shortest-bridge

在给定的二维二进制数组 A 中,存在两座岛。(岛是由四面相连的 1 形成的一个最大组。)

现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。

返回必须翻转的 0 的最小数目。(可以保证答案至少是 1 。)

示例 1:

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

示例 2:

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

示例 3:

输入:A = [[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

提示:

  • 2 <= A.length == A[0].length <= 100
  • A[i][j] == 0 或 A[i][j] == 1

二、解题思路

给一个二维的矩阵,0表示海洋1表示陆地,上面一共有两个小岛,由竖连接着的1组成的。现在填多少格子让两个小岛连在一起。这道题可以看成多起点多终点的最短路径问题。这种情况我们可以使用BFS(广度优先搜索),把起点全部push到队列里面去,下一步走到终点上的放就找到路径了,就是一个BFS找最短路径的问题。前提是知道哪部分是起点,哪部分是终点。起点我们可以使用DFS来找,找到小岛后标记成2。然后往外扩展,每次往外扩一层,直到碰到1为止。

以题目中的示例2为例,如上图左上角第二个。使用DFS找到第1个小岛后标记成2,放到queue里去,这是起点。然后用BFS去扩展,使小岛面积不断的扩大,每次往外扩一层,直到碰到1为止。上图当扩展到第3步即第3层的时候碰到1了,说明找到了路径了,即两个岛的最短距离为3-1=2。

三、解题方法

3.1 Java实现

public class Solution {
    public int shortestBridge(int[][] grid) {
        // 用来存入第一个岛屿的坐标
        Queue<Pair> queue = new LinkedList();
        boolean found = false;
        for (int i = 0; !found && i < grid.length; i++) {
            for (int j = 0; !found && j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, j, i, queue);
                    found = true;
                }
            }
        }

        int steps = 0;
        int[] dirs = new int[]{0, 1, 0, -1, 0};
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- != 0) {
                Pair p = queue.poll();
                int x = p.x;
                int y = p.y;
                for (int i = 0; i < 4; i++) {
                    int tx = x + dirs[i];
                    int ty = y + dirs[i + 1];
                    if (tx < 0 || ty < 0 || tx >= grid[0].length || ty >= grid.length || grid[ty][tx] == 2) {
                        continue;
                    }
                    if (grid[ty][tx] == 1) {
                        return steps;
                    }
                    grid[ty][tx] = 2;
                    queue.add(new Pair(tx, ty));
                }
            }
            steps++;
        }

        return -1;
    }

    private void dfs(int[][] grid, int x, int y, Queue<Pair> queue) {
        if (x < 0 || y < 0 || x >= grid[0].length || y >= grid.length || grid[y][x] != 1) {
            return;
        }
        grid[y][x] = 2;
        queue.add(new Pair(x, y));
        dfs(grid, x - 1, y, queue);
        dfs(grid, x, y - 1, queue);
        dfs(grid, x + 1, y, queue);
        dfs(grid, x, y + 1, queue);
    }

    class Pair {
        int x;
        int y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

}

四、总结小记

  • 2022/6/7 要总结一些模板

标签:Bridge,tx,ty,int,queue,length,grid,leetcode,Shortest
来源: https://www.cnblogs.com/okokabcd/p/16353864.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有