|
70 | 70 |
|
71 | 71 | <!-- 这里可写通用的实现逻辑 -->
|
72 | 72 |
|
| 73 | +**方法一:脑筋急转弯** |
| 74 | + |
| 75 | +观察发现,我们总是可以通过把角落相邻的两个陆地变成水,使得岛屿分离。因此,答案只可能是 0,1 或 2。 |
| 76 | + |
| 77 | +我们跑一遍 DFS,统计当前岛屿的数量,如果数量不等于 $1$,也就是说不满足恰好只有一座岛屿,那么答案就是 0。 |
| 78 | + |
| 79 | +否则,我们遍历每一块陆地,把它变成水,然后再跑一遍 DFS,看看岛屿的数量是否不等于 1,如果不等于 1,说明这块陆地变成水后,岛屿分离了,答案就是 1。 |
| 80 | + |
| 81 | +遍历结束,说明必须要把两块陆地变成水,才能使得岛屿分离,因此答案就是 2。 |
| 82 | + |
| 83 | +时间复杂度 $O(m^2\times n^2)$,其中 $m$ 和 $n$ 分别是 `grid` 的行数和列数。 |
| 84 | + |
73 | 85 | <!-- tabs:start -->
|
74 | 86 |
|
75 | 87 | ### **Python3**
|
76 | 88 |
|
77 | 89 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 90 |
|
79 | 91 | ```python
|
80 |
| - |
| 92 | +class Solution: |
| 93 | + def minDays(self, grid: List[List[int]]) -> int: |
| 94 | + if self.count(grid) != 1: |
| 95 | + return 0 |
| 96 | + m, n = len(grid), len(grid[0]) |
| 97 | + for i in range(m): |
| 98 | + for j in range(n): |
| 99 | + if grid[i][j] == 1: |
| 100 | + grid[i][j] = 0 |
| 101 | + if self.count(grid) != 1: |
| 102 | + return 1 |
| 103 | + grid[i][j] = 1 |
| 104 | + return 2 |
| 105 | + |
| 106 | + def count(self, grid): |
| 107 | + def dfs(i, j): |
| 108 | + grid[i][j] = 2 |
| 109 | + for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]: |
| 110 | + x, y = i + a, j + b |
| 111 | + if 0 <= x < m and 0 <= y < n and grid[x][y] == 1: |
| 112 | + dfs(x, y) |
| 113 | + |
| 114 | + m, n = len(grid), len(grid[0]) |
| 115 | + cnt = 0 |
| 116 | + for i in range(m): |
| 117 | + for j in range(n): |
| 118 | + if grid[i][j] == 1: |
| 119 | + dfs(i, j) |
| 120 | + cnt += 1 |
| 121 | + for i in range(m): |
| 122 | + for j in range(n): |
| 123 | + if grid[i][j] == 2: |
| 124 | + grid[i][j] = 1 |
| 125 | + return cnt |
81 | 126 | ```
|
82 | 127 |
|
83 | 128 | ### **Java**
|
84 | 129 |
|
85 | 130 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
86 | 131 |
|
87 | 132 | ```java
|
| 133 | +class Solution { |
| 134 | + private static final int[] DIRS = new int[] {-1, 0, 1, 0, -1}; |
| 135 | + private int[][] grid; |
| 136 | + private int m; |
| 137 | + private int n; |
| 138 | + |
| 139 | + public int minDays(int[][] grid) { |
| 140 | + this.grid = grid; |
| 141 | + m = grid.length; |
| 142 | + n = grid[0].length; |
| 143 | + if (count() != 1) { |
| 144 | + return 0; |
| 145 | + } |
| 146 | + for (int i = 0; i < m; ++i) { |
| 147 | + for (int j = 0; j < n; ++j) { |
| 148 | + if (grid[i][j] == 1) { |
| 149 | + grid[i][j] = 0; |
| 150 | + if (count() != 1) { |
| 151 | + return 1; |
| 152 | + } |
| 153 | + grid[i][j] = 1; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + return 2; |
| 158 | + } |
| 159 | + |
| 160 | + private int count() { |
| 161 | + int cnt = 0; |
| 162 | + for (int i = 0; i < m; ++i) { |
| 163 | + for (int j = 0; j < n; ++j) { |
| 164 | + if (grid[i][j] == 1) { |
| 165 | + dfs(i, j); |
| 166 | + ++cnt; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + for (int i = 0; i < m; ++i) { |
| 171 | + for (int j = 0; j < n; ++j) { |
| 172 | + if (grid[i][j] == 2) { |
| 173 | + grid[i][j] = 1; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + return cnt; |
| 178 | + } |
| 179 | + |
| 180 | + private void dfs(int i, int j) { |
| 181 | + grid[i][j] = 2; |
| 182 | + for (int k = 0; k < 4; ++k) { |
| 183 | + int x = i + DIRS[k], y = j + DIRS[k + 1]; |
| 184 | + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) { |
| 185 | + dfs(x, y); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +### **C++** |
| 193 | + |
| 194 | +```cpp |
| 195 | +class Solution { |
| 196 | +public: |
| 197 | + const vector<int> dirs = {-1, 0, 1, 0, -1}; |
| 198 | + int m, n; |
| 199 | + |
| 200 | + int minDays(vector<vector<int>>& grid) { |
| 201 | + m = grid.size(), n = grid[0].size(); |
| 202 | + if (count(grid) != 1) { |
| 203 | + return 0; |
| 204 | + } |
| 205 | + for (int i = 0; i < m; ++i) { |
| 206 | + for (int j = 0; j < n; ++j) { |
| 207 | + if (grid[i][j] == 1) { |
| 208 | + grid[i][j] = 0; |
| 209 | + if (count(grid) != 1) { |
| 210 | + return 1; |
| 211 | + } |
| 212 | + grid[i][j] = 1; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + return 2; |
| 217 | + } |
| 218 | + |
| 219 | + int count(vector<vector<int>>& grid) { |
| 220 | + int cnt = 0; |
| 221 | + for (int i = 0; i < m; ++i) { |
| 222 | + for (int j = 0; j < n; ++j) { |
| 223 | + if (grid[i][j] == 1) { |
| 224 | + dfs(i, j, grid); |
| 225 | + ++cnt; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + for (int i = 0; i < m; ++i) { |
| 230 | + for (int j = 0; j < n; ++j) { |
| 231 | + if (grid[i][j] == 2) { |
| 232 | + grid[i][j] = 1; |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + return cnt; |
| 237 | + } |
| 238 | + |
| 239 | + void dfs(int i, int j, vector<vector<int>>& grid) { |
| 240 | + grid[i][j] = 2; |
| 241 | + for (int k = 0; k < 4; ++k) { |
| 242 | + int x = i + dirs[k], y = j + dirs[k + 1]; |
| 243 | + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) { |
| 244 | + dfs(x, y, grid); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | +}; |
| 249 | +``` |
88 | 250 |
|
| 251 | +### **Go** |
| 252 | + |
| 253 | +```go |
| 254 | +func minDays(grid [][]int) int { |
| 255 | + m, n := len(grid), len(grid[0]) |
| 256 | + dirs := []int{-1, 0, 1, 0, -1} |
| 257 | + |
| 258 | + var dfs func(i, j int) |
| 259 | + dfs = func(i, j int) { |
| 260 | + grid[i][j] = 2 |
| 261 | + for k := 0; k < 4; k++ { |
| 262 | + x, y := i+dirs[k], j+dirs[k+1] |
| 263 | + if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 { |
| 264 | + dfs(x, y) |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + count := func() int { |
| 270 | + cnt := 0 |
| 271 | + for i := 0; i < m; i++ { |
| 272 | + for j := 0; j < n; j++ { |
| 273 | + if grid[i][j] == 1 { |
| 274 | + dfs(i, j) |
| 275 | + cnt++ |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + for i := 0; i < m; i++ { |
| 280 | + for j := 0; j < n; j++ { |
| 281 | + if grid[i][j] == 2 { |
| 282 | + grid[i][j] = 1 |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + return cnt |
| 287 | + } |
| 288 | + |
| 289 | + if count() != 1 { |
| 290 | + return 0 |
| 291 | + } |
| 292 | + for i := 0; i < m; i++ { |
| 293 | + for j := 0; j < n; j++ { |
| 294 | + if grid[i][j] == 1 { |
| 295 | + grid[i][j] = 0 |
| 296 | + if count() != 1 { |
| 297 | + return 1 |
| 298 | + } |
| 299 | + grid[i][j] = 1 |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + return 2 |
| 304 | +} |
89 | 305 | ```
|
90 | 306 |
|
91 | 307 | ### **...**
|
|
0 commit comments