|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 56 | +**方法一:BFS** |
| 57 | + |
| 58 | +我们注意到,题目中棋盘的大小最大为 $8 \times 8$,因此,我们可以尝试枚举所有的空余位置作为下一步放置黑棋的位置,然后使用广度优先搜索的方法计算在该位置下可以翻转的白棋的数量,找出最大值即可。 |
| 59 | + |
| 60 | +我们定义一个函数 $bfs(i, j)$,表示在棋盘上放置黑棋在 $(i, j)$ 位置后,可以翻转的白棋的数量。 |
| 61 | + |
| 62 | +在函数中,我们使用队列来进行广度优先搜索,初始时将 $(i, j)$ 放入队列中,然后不断取出队首位置,遍历棋盘的八个方向,如果该方向上是一段连续的白棋,且在末尾是黑棋,则将该黑棋之前的所有白棋都可以翻转,将这些白棋的位置放入队列中,继续进行广度优先搜索。最后,我们返回可以翻转的白棋的数量。 |
| 63 | + |
| 64 | +时间复杂度 $O(m^2 \times n^2 \times \max(m, n))$,空间复杂度 $O(m^2 \times n^2)$。其中 $m$ 和 $n$ 分别是棋盘的行数和列数。 |
| 65 | + |
56 | 66 | <!-- tabs:start -->
|
57 | 67 |
|
58 | 68 | ### **Python3**
|
59 | 69 |
|
60 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 71 |
|
62 | 72 | ```python
|
| 73 | +class Solution: |
| 74 | + def flipChess(self, chessboard: List[str]) -> int: |
| 75 | + def bfs(i: int, j: int) -> int: |
| 76 | + q = deque([(i, j)]) |
| 77 | + g = [list(row) for row in chessboard] |
| 78 | + cnt = 0 |
| 79 | + while q: |
| 80 | + i, j = q.popleft() |
| 81 | + for a, b in dirs: |
| 82 | + x, y = i + a, j + b |
| 83 | + while 0 <= x < m and 0 <= y < n and g[x][y] == "O": |
| 84 | + x, y = x + a, y + b |
| 85 | + if 0 <= x < m and 0 <= y < n and g[x][y] == "X": |
| 86 | + x, y = x - a, y - b |
| 87 | + cnt += max(abs(x - i), abs(y - j)) |
| 88 | + while x != i or y != j: |
| 89 | + g[x][y] = "X" |
| 90 | + q.append((x, y)) |
| 91 | + x, y = x - a, y - b |
| 92 | + return cnt |
| 93 | + |
| 94 | + m, n = len(chessboard), len(chessboard[0]) |
| 95 | + dirs = [(a, b) for a in range(-1, 2) for b in range(-1, 2) if a != 0 or b != 0] |
| 96 | + return max( |
| 97 | + bfs(i, j) for i in range(m) for j in range(n) if chessboard[i][j] == "." |
| 98 | + ) |
63 | 99 |
|
64 | 100 | ```
|
65 | 101 |
|
|
68 | 104 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 105 |
|
70 | 106 | ```java
|
| 107 | +class Solution { |
| 108 | + private int m; |
| 109 | + private int n; |
| 110 | + private String[] chessboard; |
| 111 | + |
| 112 | + public int flipChess(String[] chessboard) { |
| 113 | + m = chessboard.length; |
| 114 | + n = chessboard[0].length(); |
| 115 | + this.chessboard = chessboard; |
| 116 | + int ans = 0; |
| 117 | + for (int i = 0; i < m; ++i) { |
| 118 | + for (int j = 0; j < n; ++j) { |
| 119 | + if (chessboard[i].charAt(j) == '.') { |
| 120 | + ans = Math.max(ans, bfs(i, j)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | + |
| 127 | + private int bfs(int i, int j) { |
| 128 | + Deque<int[]> q = new ArrayDeque<>(); |
| 129 | + q.offer(new int[] {i, j}); |
| 130 | + char[][] g = new char[m][0]; |
| 131 | + for (int k = 0; k < m; ++k) { |
| 132 | + g[k] = chessboard[k].toCharArray(); |
| 133 | + } |
| 134 | + int cnt = 0; |
| 135 | + while (!q.isEmpty()) { |
| 136 | + var p = q.poll(); |
| 137 | + i = p[0]; |
| 138 | + j = p[1]; |
| 139 | + for (int a = -1; a <= 1; ++a) { |
| 140 | + for (int b = -1; b <= 1; ++b) { |
| 141 | + if (a == 0 && b == 0) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + int x = i + a, y = j + b; |
| 145 | + while (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'O') { |
| 146 | + x += a; |
| 147 | + y += b; |
| 148 | + } |
| 149 | + if (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'X') { |
| 150 | + x -= a; |
| 151 | + y -= b; |
| 152 | + cnt += Math.max(Math.abs(x - i), Math.abs(y - j)); |
| 153 | + while (x != i || y != j) { |
| 154 | + g[x][y] = 'X'; |
| 155 | + q.offer(new int[] {x, y}); |
| 156 | + x -= a; |
| 157 | + y -= b; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return cnt; |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### **C++** |
| 169 | + |
| 170 | +```cpp |
| 171 | +class Solution { |
| 172 | +public: |
| 173 | + int flipChess(vector<string>& chessboard) { |
| 174 | + int m = chessboard.size(); |
| 175 | + int n = chessboard[0].size(); |
| 176 | + auto bfs = [&](int i, int j) -> int { |
| 177 | + queue<pair<int, int>> q; |
| 178 | + q.emplace(i, j); |
| 179 | + auto g = chessboard; |
| 180 | + int cnt = 0; |
| 181 | + while (q.size()) { |
| 182 | + auto p = q.front(); |
| 183 | + q.pop(); |
| 184 | + i = p.first; |
| 185 | + j = p.second; |
| 186 | + for (int a = -1; a <= 1; ++a) { |
| 187 | + for (int b = -1; b <= 1; ++b) { |
| 188 | + if (a == 0 && b == 0) { |
| 189 | + continue; |
| 190 | + } |
| 191 | + int x = i + a, y = j + b; |
| 192 | + while (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'O') { |
| 193 | + x += a; |
| 194 | + y += b; |
| 195 | + } |
| 196 | + if (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'X') { |
| 197 | + x -= a; |
| 198 | + y -= b; |
| 199 | + cnt += max(abs(x - i), abs(y - j)); |
| 200 | + while (x != i || y != j) { |
| 201 | + g[x][y] = 'X'; |
| 202 | + q.emplace(x, y); |
| 203 | + x -= a; |
| 204 | + y -= b; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return cnt; |
| 211 | + }; |
| 212 | + |
| 213 | + int ans = 0; |
| 214 | + for (int i = 0; i < m; ++i) { |
| 215 | + for (int j = 0; j < n; ++j) { |
| 216 | + if (chessboard[i][j] == '.') { |
| 217 | + ans = max(ans, bfs(i, j)); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + return ans; |
| 222 | + } |
| 223 | +}; |
| 224 | +``` |
| 225 | + |
| 226 | +### **Go** |
| 227 | + |
| 228 | +```go |
| 229 | +func flipChess(chessboard []string) (ans int) { |
| 230 | + m, n := len(chessboard), len(chessboard[0]) |
| 231 | + bfs := func(i, j int) int { |
| 232 | + q := [][2]int{{i, j}} |
| 233 | + g := make([][]byte, m) |
| 234 | + for i := range g { |
| 235 | + g[i] = make([]byte, n) |
| 236 | + copy(g[i], []byte(chessboard[i])) |
| 237 | + } |
| 238 | + cnt := 0 |
| 239 | + for len(q) > 0 { |
| 240 | + p := q[0] |
| 241 | + q = q[1:] |
| 242 | + i, j = p[0], p[1] |
| 243 | + for a := -1; a <= 1; a++ { |
| 244 | + for b := -1; b <= 1; b++ { |
| 245 | + if a == 0 && b == 0 { |
| 246 | + continue |
| 247 | + } |
| 248 | + x, y := i+a, j+b |
| 249 | + for x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'O' { |
| 250 | + x, y = x+a, y+b |
| 251 | + } |
| 252 | + if x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'X' { |
| 253 | + x -= a |
| 254 | + y -= b |
| 255 | + cnt += max(abs(x-i), abs(y-j)) |
| 256 | + for x != i || y != j { |
| 257 | + g[x][y] = 'X' |
| 258 | + q = append(q, [2]int{x, y}) |
| 259 | + x -= a |
| 260 | + y -= b |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + return cnt |
| 267 | + } |
| 268 | + for i, row := range chessboard { |
| 269 | + for j, c := range row { |
| 270 | + if c == '.' { |
| 271 | + ans = max(ans, bfs(i, j)) |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + return |
| 276 | +} |
| 277 | + |
| 278 | +func abs(x int) int { |
| 279 | + if x < 0 { |
| 280 | + return -x |
| 281 | + } |
| 282 | + return x |
| 283 | +} |
71 | 284 |
|
| 285 | +func max(a, b int) int { |
| 286 | + if a > b { |
| 287 | + return a |
| 288 | + } |
| 289 | + return b |
| 290 | +} |
72 | 291 | ```
|
73 | 292 |
|
74 | 293 | ### **...**
|
|
0 commit comments