|
47 | 47 |
|
48 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 49 |
|
| 50 | +DFS & BFS。 |
| 51 | + |
| 52 | +先通过 DFS 将其中一个岛屿的所有 1 置为 2,并将这个岛屿所有坐标点放入队列中;然后通过 BFS 一层层向外扩散,直至碰到另一个岛屿。 |
| 53 | + |
50 | 54 | <!-- tabs:start -->
|
51 | 55 |
|
52 | 56 | ### **Python3**
|
53 | 57 |
|
54 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 59 |
|
56 | 60 | ```python
|
| 61 | +class Solution: |
| 62 | + def shortestBridge(self, grid: List[List[int]]) -> int: |
| 63 | + def find(): |
| 64 | + for i in range(m): |
| 65 | + for j in range(n): |
| 66 | + if grid[i][j] == 1: |
| 67 | + return i, j |
57 | 68 |
|
| 69 | + def dfs(i, j): |
| 70 | + q.append((i, j)) |
| 71 | + grid[i][j] = 2 |
| 72 | + for a, b in dirs: |
| 73 | + x, y = i + a, j + b |
| 74 | + if 0 <= x < m and 0 <= y < n and grid[x][y] == 1: |
| 75 | + dfs(x, y) |
| 76 | + |
| 77 | + m, n = len(grid), len(grid[0]) |
| 78 | + q = deque() |
| 79 | + dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]] |
| 80 | + i, j = find() |
| 81 | + dfs(i, j) |
| 82 | + ans = -1 |
| 83 | + while q: |
| 84 | + ans += 1 |
| 85 | + for _ in range(len(q), 0, -1): |
| 86 | + i, j = q.popleft() |
| 87 | + for a, b in dirs: |
| 88 | + x, y = i + a, j + b |
| 89 | + if 0 <= x < m and 0 <= y < n: |
| 90 | + if grid[x][y] == 1: |
| 91 | + return ans |
| 92 | + if grid[x][y] == 0: |
| 93 | + grid[x][y] = 2 |
| 94 | + q.append((x, y)) |
| 95 | + return 0 |
58 | 96 | ```
|
59 | 97 |
|
60 | 98 | ### **Java**
|
61 | 99 |
|
62 | 100 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 101 |
|
64 | 102 | ```java
|
| 103 | +class Solution { |
| 104 | + private int[][] grid; |
| 105 | + private int[] dirs = {-1, 0, 1, 0, -1}; |
| 106 | + private int m; |
| 107 | + private int n; |
| 108 | + |
| 109 | + public int shortestBridge(int[][] grid) { |
| 110 | + m = grid.length; |
| 111 | + n = grid[0].length; |
| 112 | + this.grid = grid; |
| 113 | + int[] start = find(); |
| 114 | + Queue<int[]> q = new LinkedList<>(); |
| 115 | + dfs(start[0], start[1], q); |
| 116 | + int ans = -1; |
| 117 | + while (!q.isEmpty()) { |
| 118 | + ++ans; |
| 119 | + for (int k = q.size(); k > 0; --k) { |
| 120 | + int[] p = q.poll(); |
| 121 | + for (int i = 0; i < 4; ++i) { |
| 122 | + int x = p[0] + dirs[i]; |
| 123 | + int y = p[1] + dirs[i + 1]; |
| 124 | + if (x >= 0 && x < m && y >= 0 && y < n) { |
| 125 | + if (grid[x][y] == 1) { |
| 126 | + return ans; |
| 127 | + } |
| 128 | + if (grid[x][y] == 0) { |
| 129 | + grid[x][y] = 2; |
| 130 | + q.offer(new int[]{x, y}); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + private void dfs(int i, int j, Queue<int[]> q) { |
| 140 | + grid[i][j] = 2; |
| 141 | + q.offer(new int[]{i, j}); |
| 142 | + for (int k = 0; k < 4; ++k) { |
| 143 | + int x = i + dirs[k]; |
| 144 | + int y = j + dirs[k + 1]; |
| 145 | + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) { |
| 146 | + dfs(x, y, q); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private int[] find() { |
| 152 | + for (int i = 0; i < m; ++i) { |
| 153 | + for (int j = 0; j < n; ++j) { |
| 154 | + if (grid[i][j] == 1) { |
| 155 | + return new int[]{i, j}; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return new int[]{0, 0}; |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### **C++** |
| 165 | + |
| 166 | +```cpp |
| 167 | +class Solution { |
| 168 | +public: |
| 169 | + vector<int> dirs = {-1, 0, 1, 0, -1}; |
| 170 | + |
| 171 | + int shortestBridge(vector<vector<int>>& grid) { |
| 172 | + vector<int> start = find(grid); |
| 173 | + queue<vector<int>> q; |
| 174 | + dfs(start[0], start[1], q, grid); |
| 175 | + int ans = -1; |
| 176 | + while (!q.empty()) |
| 177 | + { |
| 178 | + ++ans; |
| 179 | + for (int k = q.size(); k > 0; --k) |
| 180 | + { |
| 181 | + auto p = q.front(); |
| 182 | + q.pop(); |
| 183 | + for (int i = 0; i < 4; ++i) |
| 184 | + { |
| 185 | + int x = p[0] + dirs[i]; |
| 186 | + int y = p[1] + dirs[i + 1]; |
| 187 | + if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) |
| 188 | + { |
| 189 | + if (grid[x][y] == 1) return ans; |
| 190 | + if (grid[x][y] == 0) |
| 191 | + { |
| 192 | + grid[x][y] = 2; |
| 193 | + q.push({x, y}); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + return 0; |
| 200 | + } |
| 201 | + |
| 202 | + void dfs(int i, int j, queue<vector<int>>& q, vector<vector<int>>& grid) { |
| 203 | + grid[i][j] = 2; |
| 204 | + q.push({i, j}); |
| 205 | + for (int k = 0; k < 4; ++k) |
| 206 | + { |
| 207 | + int x = i + dirs[k]; |
| 208 | + int y = j + dirs[k + 1]; |
| 209 | + if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1) |
| 210 | + dfs(x, y, q, grid); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + vector<int> find(vector<vector<int>>& grid) { |
| 215 | + for (int i = 0; i < grid.size(); ++i) |
| 216 | + for (int j = 0; j < grid[0].size(); ++j) |
| 217 | + if (grid[i][j] == 1) |
| 218 | + return {i, j}; |
| 219 | + return {0, 0}; |
| 220 | + } |
| 221 | +}; |
| 222 | +``` |
| 223 | +
|
| 224 | +### **Go** |
65 | 225 |
|
| 226 | +```go |
| 227 | +func shortestBridge(grid [][]int) int { |
| 228 | + m, n := len(grid), len(grid[0]) |
| 229 | + find := func() []int { |
| 230 | + for i := 0; i < m; i++ { |
| 231 | + for j := 0; j < n; j++ { |
| 232 | + if grid[i][j] == 1 { |
| 233 | + return []int{i, j} |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + return []int{0, 0} |
| 238 | + } |
| 239 | + start := find() |
| 240 | + var q [][]int |
| 241 | + dirs := []int{-1, 0, 1, 0, -1} |
| 242 | + var dfs func(i, j int) |
| 243 | + dfs = func(i, j int) { |
| 244 | + grid[i][j] = 2 |
| 245 | + q = append(q, []int{i, j}) |
| 246 | + for k := 0; k < 4; k++ { |
| 247 | + x, y := i+dirs[k], j+dirs[k+1] |
| 248 | + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 { |
| 249 | + dfs(x, y) |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + dfs(start[0], start[1]) |
| 254 | + ans := -1 |
| 255 | + for len(q) > 0 { |
| 256 | + ans++ |
| 257 | + for k := len(q); k > 0; k-- { |
| 258 | + p := q[0] |
| 259 | + q = q[1:] |
| 260 | + for i := 0; i < 4; i++ { |
| 261 | + x, y := p[0]+dirs[i], p[1]+dirs[i+1] |
| 262 | + if x >= 0 && x < m && y >= 0 && y < n { |
| 263 | + if grid[x][y] == 1 { |
| 264 | + return ans |
| 265 | + } |
| 266 | + if grid[x][y] == 0 { |
| 267 | + grid[x][y] = 2 |
| 268 | + q = append(q, []int{x, y}) |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + return 0 |
| 275 | +} |
66 | 276 | ```
|
67 | 277 |
|
68 | 278 | ### **...**
|
|
0 commit comments