|
64 | 64 |
|
65 | 65 | <!-- 这里可写通用的实现逻辑 -->
|
66 | 66 |
|
| 67 | +**方法一:DFS** |
| 68 | + |
| 69 | +我们记 $click = (i, j)$,如果 $board[i][j]$ 等于 `'M'`,那么直接将 $board[i][j]$ 的值改为 `'X'` 即可。否则,我们需要统计 $board[i][j]$ 周围的地雷数量 $cnt$,如果 $cnt$ 不为 $0$,那么将 $board[i][j]$ 的值改为 $cnt$ 的字符串形式。否则,将 $board[i][j]$ 的值改为 `'B'`,并且递归地搜索处理 $board[i][j]$ 周围的未挖出的方块。 |
| 70 | + |
| 71 | +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是二维数组 $board$ 的行数和列数。 |
| 72 | + |
67 | 73 | <!-- tabs:start -->
|
68 | 74 |
|
69 | 75 | ### **Python3**
|
70 | 76 |
|
71 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
72 | 78 |
|
73 | 79 | ```python
|
| 80 | +class Solution: |
| 81 | + def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]: |
| 82 | + def dfs(i: int, j: int): |
| 83 | + cnt = 0 |
| 84 | + for x in range(i - 1, i + 2): |
| 85 | + for y in range(j - 1, j + 2): |
| 86 | + if 0 <= x < m and 0 <= y < n and board[x][y] == "M": |
| 87 | + cnt += 1 |
| 88 | + if cnt: |
| 89 | + board[i][j] = str(cnt) |
| 90 | + else: |
| 91 | + board[i][j] = "B" |
| 92 | + for x in range(i - 1, i + 2): |
| 93 | + for y in range(j - 1, j + 2): |
| 94 | + if 0 <= x < m and 0 <= y < n and board[x][y] == "E": |
| 95 | + dfs(x, y) |
74 | 96 |
|
| 97 | + m, n = len(board), len(board[0]) |
| 98 | + i, j = click |
| 99 | + if board[i][j] == "M": |
| 100 | + board[i][j] = "X" |
| 101 | + else: |
| 102 | + dfs(i, j) |
| 103 | + return board |
75 | 104 | ```
|
76 | 105 |
|
77 | 106 | ### **Java**
|
78 | 107 |
|
79 | 108 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
80 | 109 |
|
81 | 110 | ```java
|
| 111 | +class Solution { |
| 112 | + private char[][] board; |
| 113 | + private int m; |
| 114 | + private int n; |
| 115 | + |
| 116 | + public char[][] updateBoard(char[][] board, int[] click) { |
| 117 | + m = board.length; |
| 118 | + n = board[0].length; |
| 119 | + this.board = board; |
| 120 | + int i = click[0], j = click[1]; |
| 121 | + if (board[i][j] == 'M') { |
| 122 | + board[i][j] = 'X'; |
| 123 | + } else { |
| 124 | + dfs(i, j); |
| 125 | + } |
| 126 | + return board; |
| 127 | + } |
| 128 | + |
| 129 | + private void dfs(int i, int j) { |
| 130 | + int cnt = 0; |
| 131 | + for (int x = i - 1; x <= i + 1; ++x) { |
| 132 | + for (int y = j - 1; y <= j + 1; ++y) { |
| 133 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') { |
| 134 | + ++cnt; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + if (cnt > 0) { |
| 139 | + board[i][j] = (char) (cnt + '0'); |
| 140 | + } else { |
| 141 | + board[i][j] = 'B'; |
| 142 | + for (int x = i - 1; x <= i + 1; ++x) { |
| 143 | + for (int y = j - 1; y <= j + 1; ++y) { |
| 144 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E') { |
| 145 | + dfs(x, y); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### **C++** |
| 155 | + |
| 156 | +```cpp |
| 157 | +class Solution { |
| 158 | +public: |
| 159 | + vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) { |
| 160 | + int m = board.size(), n = board[0].size(); |
| 161 | + int i = click[0], j = click[1]; |
| 162 | + |
| 163 | + function<void(int, int)> dfs = [&](int i, int j) { |
| 164 | + int cnt = 0; |
| 165 | + for (int x = i - 1; x <= i + 1; ++x) { |
| 166 | + for (int y = j - 1; y <= j + 1; ++y) { |
| 167 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') { |
| 168 | + ++cnt; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + if (cnt) { |
| 173 | + board[i][j] = cnt + '0'; |
| 174 | + } else { |
| 175 | + board[i][j] = 'B'; |
| 176 | + for (int x = i - 1; x <= i + 1; ++x) { |
| 177 | + for (int y = j - 1; y <= j + 1; ++y) { |
| 178 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E') { |
| 179 | + dfs(x, y); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + }; |
| 185 | + |
| 186 | + if (board[i][j] == 'M') { |
| 187 | + board[i][j] = 'X'; |
| 188 | + } else { |
| 189 | + dfs(i, j); |
| 190 | + } |
| 191 | + return board; |
| 192 | + } |
| 193 | +}; |
| 194 | +``` |
| 195 | + |
| 196 | +### **Go** |
| 197 | + |
| 198 | +```go |
| 199 | +func updateBoard(board [][]byte, click []int) [][]byte { |
| 200 | + m, n := len(board), len(board[0]) |
| 201 | + i, j := click[0], click[1] |
| 202 | + |
| 203 | + var dfs func(i, j int) |
| 204 | + dfs = func(i, j int) { |
| 205 | + cnt := 0 |
| 206 | + for x := i - 1; x <= i+1; x++ { |
| 207 | + for y := j - 1; y <= j+1; y++ { |
| 208 | + if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M' { |
| 209 | + cnt++ |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + if cnt > 0 { |
| 214 | + board[i][j] = byte(cnt + '0') |
| 215 | + return |
| 216 | + } |
| 217 | + board[i][j] = 'B' |
| 218 | + for x := i - 1; x <= i+1; x++ { |
| 219 | + for y := j - 1; y <= j+1; y++ { |
| 220 | + if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E' { |
| 221 | + dfs(x, y) |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + if board[i][j] == 'M' { |
| 228 | + board[i][j] = 'X' |
| 229 | + } else { |
| 230 | + dfs(i, j) |
| 231 | + } |
| 232 | + return board |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +### **TypeScript** |
| 237 | + |
| 238 | +```ts |
| 239 | +function updateBoard(board: string[][], click: number[]): string[][] { |
| 240 | + const m = board.length; |
| 241 | + const n = board[0].length; |
| 242 | + const [i, j] = click; |
| 243 | + |
| 244 | + const dfs = (i: number, j: number) => { |
| 245 | + let cnt = 0; |
| 246 | + for (let x = i - 1; x <= i + 1; ++x) { |
| 247 | + for (let y = j - 1; y <= j + 1; ++y) { |
| 248 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] === 'M') { |
| 249 | + ++cnt; |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + if (cnt > 0) { |
| 254 | + board[i][j] = cnt.toString(); |
| 255 | + return; |
| 256 | + } |
| 257 | + board[i][j] = 'B'; |
| 258 | + for (let x = i - 1; x <= i + 1; ++x) { |
| 259 | + for (let y = j - 1; y <= j + 1; ++y) { |
| 260 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] === 'E') { |
| 261 | + dfs(x, y); |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + }; |
82 | 266 |
|
| 267 | + if (board[i][j] === 'M') { |
| 268 | + board[i][j] = 'X'; |
| 269 | + } else { |
| 270 | + dfs(i, j); |
| 271 | + } |
| 272 | + return board; |
| 273 | +} |
83 | 274 | ```
|
84 | 275 |
|
85 | 276 | ### **...**
|
|
0 commit comments