首页 > 其他 > 详细

LeetCode 934. Shortest Bridge

时间:2019-11-26 14:30:18      阅读:102      评论:0      收藏:0      [点我收藏+]

原题链接在这里: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 }

 

LeetCode 934. Shortest Bridge

原文:https://www.cnblogs.com/Dylan-Java-NYC/p/11934715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!