Skip to content

Commit e605c0c

Browse files
committed
feat: add solutions to lcp problem: No.41
LCP 41. 黑白翻转棋
1 parent aa91804 commit e605c0c

File tree

5 files changed

+419
-0
lines changed

5 files changed

+419
-0
lines changed

lcp/LCP 41. 黑白翻转棋/README.md

+219
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,49 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

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+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```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+
)
6399

64100
```
65101

@@ -68,7 +104,190 @@
68104
<!-- 这里可写当前语言的特殊实现逻辑 -->
69105

70106
```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+
}
71284

285+
func max(a, b int) int {
286+
if a > b {
287+
return a
288+
}
289+
return b
290+
}
72291
```
73292

74293
### **...**
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public:
3+
int flipChess(vector<string>& chessboard) {
4+
int m = chessboard.size();
5+
int n = chessboard[0].size();
6+
auto bfs = [&](int i, int j) -> int {
7+
queue<pair<int, int>> q;
8+
q.emplace(i, j);
9+
auto g = chessboard;
10+
int cnt = 0;
11+
while (q.size()) {
12+
auto p = q.front();
13+
q.pop();
14+
i = p.first;
15+
j = p.second;
16+
for (int a = -1; a <= 1; ++a) {
17+
for (int b = -1; b <= 1; ++b) {
18+
if (a == 0 && b == 0) {
19+
continue;
20+
}
21+
int x = i + a, y = j + b;
22+
while (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'O') {
23+
x += a;
24+
y += b;
25+
}
26+
if (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'X') {
27+
x -= a;
28+
y -= b;
29+
cnt += max(abs(x - i), abs(y - j));
30+
while (x != i || y != j) {
31+
g[x][y] = 'X';
32+
q.emplace(x, y);
33+
x -= a;
34+
y -= b;
35+
}
36+
}
37+
}
38+
}
39+
}
40+
return cnt;
41+
};
42+
43+
int ans = 0;
44+
for (int i = 0; i < m; ++i) {
45+
for (int j = 0; j < n; ++j) {
46+
if (chessboard[i][j] == '.') {
47+
ans = max(ans, bfs(i, j));
48+
}
49+
}
50+
}
51+
return ans;
52+
}
53+
};
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
func flipChess(chessboard []string) (ans int) {
2+
m, n := len(chessboard), len(chessboard[0])
3+
bfs := func(i, j int) int {
4+
q := [][2]int{{i, j}}
5+
g := make([][]byte, m)
6+
for i := range g {
7+
g[i] = make([]byte, n)
8+
copy(g[i], []byte(chessboard[i]))
9+
}
10+
cnt := 0
11+
for len(q) > 0 {
12+
p := q[0]
13+
q = q[1:]
14+
i, j = p[0], p[1]
15+
for a := -1; a <= 1; a++ {
16+
for b := -1; b <= 1; b++ {
17+
if a == 0 && b == 0 {
18+
continue
19+
}
20+
x, y := i+a, j+b
21+
for x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'O' {
22+
x, y = x+a, y+b
23+
}
24+
if x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 'X' {
25+
x -= a
26+
y -= b
27+
cnt += max(abs(x-i), abs(y-j))
28+
for x != i || y != j {
29+
g[x][y] = 'X'
30+
q = append(q, [2]int{x, y})
31+
x -= a
32+
y -= b
33+
}
34+
}
35+
}
36+
}
37+
}
38+
return cnt
39+
}
40+
for i, row := range chessboard {
41+
for j, c := range row {
42+
if c == '.' {
43+
ans = max(ans, bfs(i, j))
44+
}
45+
}
46+
}
47+
return
48+
}
49+
50+
func abs(x int) int {
51+
if x < 0 {
52+
return -x
53+
}
54+
return x
55+
}
56+
57+
func max(a, b int) int {
58+
if a > b {
59+
return a
60+
}
61+
return b
62+
}

0 commit comments

Comments
 (0)