1958. 检查操作是否合法

检查操作是否合法

解法一: 枚举

go
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
func calcClean(board [][]byte, rMove int, cMove int, color byte) int {
m, n := len(board), len(board[0])
directions := []struct {
x int
y int
}{
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1},
{-1, -1},
}
ans := 0
for _, direction := range directions {
x := rMove + direction.x
y := cMove + direction.y
if x < 0 || x >= m || y < 0 || y >= n || board[x][y] == '.' || board[x][y] == color {
continue
}
cur := 1
for x >= 0 && x < m && y >= 0 && y < n && board[x][y] != '.' {
if board[x][y] == color {
ans += cur
break
}
cur++
x += direction.x
y += direction.y
}
}
return ans
}
作者

wuhunyu

发布于

2024-07-07

更新于

2025-01-15

许可协议