|
| 1 | +package LeetCodeJava.BFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/shortest-bridge/description/ |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +/** |
| 9 | + * 934. Shortest Bridge |
| 10 | + * Medium |
| 11 | + * Topics |
| 12 | + * Companies |
| 13 | + * You are given an n x n binary matrix grid where 1 represents land and 0 represents water. |
| 14 | + * |
| 15 | + * An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid. |
| 16 | + * |
| 17 | + * You may change 0's to 1's to connect the two islands to form one island. |
| 18 | + * |
| 19 | + * Return the smallest number of 0's you must flip to connect the two islands. |
| 20 | + * |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: grid = [[0,1],[1,0]] |
| 26 | + * Output: 1 |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: grid = [[0,1,0],[0,0,0],[0,0,1]] |
| 30 | + * Output: 2 |
| 31 | + * Example 3: |
| 32 | + * |
| 33 | + * Input: 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]] |
| 34 | + * Output: 1 |
| 35 | + * |
| 36 | + * |
| 37 | + * Constraints: |
| 38 | + * |
| 39 | + * n == grid.length == grid[i].length |
| 40 | + * 2 <= n <= 100 |
| 41 | + * grid[i][j] is either 0 or 1. |
| 42 | + * There are exactly two islands in grid. |
| 43 | + */ |
| 44 | +public class ShortestBridge { |
| 45 | + // V0 |
| 46 | +// public int shortestBridge(int[][] grid) { |
| 47 | +// |
| 48 | +// } |
| 49 | + |
| 50 | + // V1-1 |
| 51 | + // https://leetcode.com/problems/shortest-bridge/editorial/ |
| 52 | + // IDEA: Depth-First-Search + Breadth-First-Search |
| 53 | + private List<int[]> bfsQueue; |
| 54 | + |
| 55 | + // Recursively check the neighboring land cell of current cell grid[x][y] and |
| 56 | + // add all |
| 57 | + // land cells of island A to bfsQueue. |
| 58 | + private void dfs(int[][] grid, int x, int y, int n) { |
| 59 | + grid[x][y] = 2; |
| 60 | + bfsQueue.add(new int[] { x, y }); |
| 61 | + for (int[] pair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) { |
| 62 | + int curX = pair[0], curY = pair[1]; |
| 63 | + if (0 <= curX && curX < n && 0 <= curY && curY < n && grid[curX][curY] == 1) { |
| 64 | + dfs(grid, curX, curY, n); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Find any land cell, and we treat it as a cell of island A. |
| 70 | + public int shortestBridge_1_1(int[][] grid) { |
| 71 | + int n = grid.length; |
| 72 | + int firstX = -1, firstY = -1; |
| 73 | + for (int i = 0; i < n; i++) { |
| 74 | + for (int j = 0; j < n; j++) { |
| 75 | + if (grid[i][j] == 1) { |
| 76 | + firstX = i; |
| 77 | + firstY = j; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Add all land cells of island A to bfsQueue. |
| 84 | + bfsQueue = new ArrayList<>(); |
| 85 | + dfs(grid, firstX, firstY, n); |
| 86 | + |
| 87 | + int distance = 0; |
| 88 | + while (!bfsQueue.isEmpty()) { |
| 89 | + List<int[]> newBfs = new ArrayList<>(); |
| 90 | + for (int[] pair : bfsQueue) { |
| 91 | + int x = pair[0], y = pair[1]; |
| 92 | + for (int[] nextPair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) { |
| 93 | + int curX = nextPair[0], curY = nextPair[1]; |
| 94 | + if (0 <= curX && curX < n && 0 <= curY && curY < n) { |
| 95 | + if (grid[curX][curY] == 1) { |
| 96 | + return distance; |
| 97 | + } else if (grid[curX][curY] == 0) { |
| 98 | + newBfs.add(nextPair); |
| 99 | + grid[curX][curY] = -1; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Once we finish one round without finding land cells of island B, we will |
| 106 | + // start the next round on all water cells that are 1 cell further away from |
| 107 | + // island A and increment the distance by 1. |
| 108 | + bfsQueue = newBfs; |
| 109 | + distance++; |
| 110 | + } |
| 111 | + |
| 112 | + return distance; |
| 113 | + } |
| 114 | + |
| 115 | + // V1-2 |
| 116 | + // https://leetcode.com/problems/shortest-bridge/editorial/ |
| 117 | + // IDEA: BFS |
| 118 | + public int shortestBridge_1_2(int[][] grid) { |
| 119 | + int n = grid.length; |
| 120 | + int firstX = -1, firstY = -1; |
| 121 | + |
| 122 | + // Find any land cell, and we treat it as a cell of island A. |
| 123 | + for (int i = 0; i < n; i++) { |
| 124 | + for (int j = 0; j < n; j++) { |
| 125 | + if (grid[i][j] == 1) { |
| 126 | + firstX = i; |
| 127 | + firstY = j; |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // bfsQueue for BFS on land cells of island A; secondBfsQueue for BFS on water |
| 134 | + // cells. |
| 135 | + List<int[]> bfsQueue = new ArrayList<>(); |
| 136 | + List<int[]> secondBfsQueue = new ArrayList<>(); |
| 137 | + bfsQueue.add(new int[] { firstX, firstY }); |
| 138 | + secondBfsQueue.add(new int[] { firstX, firstY }); |
| 139 | + grid[firstX][firstY] = 2; |
| 140 | + |
| 141 | + // BFS for all land cells of island A and add them to secondBfsQueue. |
| 142 | + while (!bfsQueue.isEmpty()) { |
| 143 | + List<int[]> newBfs = new ArrayList<>(); |
| 144 | + for (int[] cell : bfsQueue) { |
| 145 | + int x = cell[0]; |
| 146 | + int y = cell[1]; |
| 147 | + for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) { |
| 148 | + int curX = next[0]; |
| 149 | + int curY = next[1]; |
| 150 | + if (curX >= 0 && curX < n && curY >= 0 && curY < n && grid[curX][curY] == 1) { |
| 151 | + newBfs.add(new int[] { curX, curY }); |
| 152 | + secondBfsQueue.add(new int[] { curX, curY }); |
| 153 | + grid[curX][curY] = 2; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + bfsQueue = newBfs; |
| 158 | + } |
| 159 | + |
| 160 | + int distance = 0; |
| 161 | + while (!secondBfsQueue.isEmpty()) { |
| 162 | + List<int[]> newBfs = new ArrayList<>(); |
| 163 | + for (int[] cell : secondBfsQueue) { |
| 164 | + int x = cell[0]; |
| 165 | + int y = cell[1]; |
| 166 | + for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) { |
| 167 | + int curX = next[0]; |
| 168 | + int curY = next[1]; |
| 169 | + if (curX >= 0 && curX < n && curY >= 0 && curY < n) { |
| 170 | + if (grid[curX][curY] == 1) { |
| 171 | + return distance; |
| 172 | + } else if (grid[curX][curY] == 0) { |
| 173 | + newBfs.add(new int[] { curX, curY }); |
| 174 | + grid[curX][curY] = -1; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Once we finish one round without finding land cells of island B, we will |
| 181 | + // start the next round on all water cells that are 1 cell further away from |
| 182 | + // island A and increment the distance by 1. |
| 183 | + secondBfsQueue = newBfs; |
| 184 | + distance++; |
| 185 | + } |
| 186 | + return distance; |
| 187 | + } |
| 188 | + |
| 189 | + // V2 |
| 190 | +} |
0 commit comments