1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
func max(nums ...int) int {
res := nums[0]
for _, val := range nums {
if val > res {
res = val
}
}
return res
}
func flipChess(chessboard []string) int {
dirs := [8][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
h, w := len(chessboard), len(chessboard[0])
var judge func(board [][]byte, x, y, dx, dy int) bool
judge = func(board [][]byte, x, y, dx, dy int) bool {
x, y = x+dx, y+dy
for x >= 0 && x < h && y >= 0 && y < w {
if board[x][y] == 'X' {
return true
}
if board[x][y] == '.' {
return false
}
x, y = x+dx, y+dy
}
return false
}
var bfs func(board [][]byte, x, y int) int
bfs = func(board [][]byte, x, y int) int {
ret := 0
var queue [][2]int
queue = append(queue, [2]int{x, y})
board[x][y] = 'X'
for len(queue) > 0 {
curX, curY := queue[0][0], queue[0][1]
queue = queue[1:]
for i := 0; i < len(dirs); i++ {
dx, dy := dirs[i][0], dirs[i][1]
if judge(board, curX, curY, dx, dy) {
nX, nY := curX+dx, curY+dy
for board[nX][nY] != 'X' {
queue = append(queue, [2]int{nX, nY})
board[nX][nY] = 'X'
nX, nY = nX+dx, nY+dy
ret++
}
}
}
}
return ret
}
ans := 0
for i := 0; i < h; i++ {
for j := 0; j < w; j++ {
if chessboard[i][j] == '.' {
board := make([][]byte, h)
for k, str := range chessboard {
board[k] = []byte(str)
}
ans = max(ans, bfs(board, i, j))
}
}
}
return ans
}
|