zoukankan      html  css  js  c++  java
  • LeetCode 934. Shortest Bridge

    原题链接在这里:https://leetcode.com/problems/shortest-bridge/

    题目:

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

    Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

    Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)

    Example 1:

    Input: [[0,1],[1,0]]
    Output: 1
    

    Example 2:

    Input: [[0,1,0],[0,0,0],[0,0,1]]
    Output: 2
    

    Example 3:

    Input: [[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]]
    Output: 1

    Note:

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

    题解:

    Find an index pointing to 1. Starting from it, iterating its neighbors and put all the islands into first set.

    Perform BFS for each index in the current set, search surroundings, it is never visited before, and it is 1, then it is a cell in the other island, return level.

    Otherwise, it is water, add it to nextSet.

    Time Complexity: O(m*n). m = A.length. n = A[0].length.

    Space: O(m*n).

    AC Java:

     1 class Solution {
     2     int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
     3     public int shortestBridge(int[][] A) {
     4         if(A == null || A.length == 0 || A[0].length == 0){
     5             return -1;
     6         }
     7         
     8         int m = A.length;
     9         int n = A[0].length;
    10         int r = 0;
    11         int c = 0;
    12         for(int i = 0; i<m; i++){
    13             for(int j = 0; j<n; j++){
    14                 if(A[i][j] == 0){
    15                     continue;
    16                 }
    17                 
    18                 r = i;
    19                 c = j;
    20             }
    21         }
    22         
    23         boolean [][] visited = new boolean[m][n];
    24         LinkedList<int []> que = new LinkedList<>();
    25         HashSet<int []> beginSet = new HashSet<>();
    26         visited[r][c] = true;
    27         que.add(new int[]{r,c});
    28         while(!que.isEmpty()){
    29             int [] cur = que.poll();
    30             beginSet.add(cur);
    31             for(int [] dir : dirs){
    32                 int x = cur[0] + dir[0];
    33                 int y = cur[1] + dir[1];
    34                 if(x<0 || x>=m || y<0 || y>=n || A[x][y]!= 1 || visited[x][y]){
    35                     continue;
    36                 }
    37                 
    38                 visited[x][y] = true;
    39                 que.add(new int[]{x, y});
    40             }
    41         }
    42         
    43         int level = 0;
    44         
    45         while(!beginSet.isEmpty()){
    46             HashSet<int []> nextSet = new HashSet<>();
    47             for(int [] node : beginSet){
    48                 for(int [] dir : dirs){
    49                     int x = node[0] + dir[0];
    50                     int y = node[1] + dir[1];
    51                     if(x<0 || x>=m || y<0 || y>=n || visited[x][y]){
    52                         continue;
    53                     }
    54                     
    55                     visited[x][y] = true;
    56                     if(A[x][y] == 1){
    57                         return level;
    58                     }
    59                     
    60                     nextSet.add(new int[]{x, y});
    61                 }
    62             }
    63             
    64             level++;
    65             beginSet = nextSet;
    66         }
    67         
    68         return -1;
    69     }
    70 }
  • 相关阅读:
    javascript中的几点说明
    repeater 绑定数组
    SQL语句之按in排序
    固定VS2005端口号
    关于asp.net中动态获取LinkButton的Text值的问题
    ASP.NET 2.0中直接得到本页面生成的HTML代码
    实现数据库的备份与还原的功能
    使用ASP.NET 2.0提供的WebResource管理内嵌资源(c#)
    [IIS]由于无法创建应用程序域,因此未能执行请求解决方案汇总
    VS2005中引用Microsoft Office COM组件
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11934715.html
Copyright © 2011-2022 走看看