【题目描述】

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

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

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

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

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


【示例】



【代码】admin

package com.company;
import java.util.*;

// 2023-04-26
class Solution {
    
    public int shortestBridge(int[][] grid) {

    }
}
public class Test {
    public static void main(String[] args) {
        new Solution().shortestBridge(new int[][] {{0, 1},{1, 0}}); // 输出: 1
        new Solution().shortestBridge(new int[][] {{0, 1, 0}, {0, 0, 0}, {0, 0, 1}}); // 输出:2
        new Solution().shortestBridge(new int[][] {{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
    }
}