-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
63 lines (62 loc) · 1.33 KB
/
Solution.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
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
func movesToChessboard(board [][]int) int {
n := len(board)
mask := (1 << n) - 1
rowMask, colMask := 0, 0
for i := 0; i < n; i++ {
rowMask |= board[0][i] << i
colMask |= board[i][0] << i
}
revRowMask := mask ^ rowMask
revColMask := mask ^ colMask
sameRow, sameCol := 0, 0
for i := 0; i < n; i++ {
curRowMask, curColMask := 0, 0
for j := 0; j < n; j++ {
curRowMask |= board[i][j] << j
curColMask |= board[j][i] << j
}
if curRowMask != rowMask && curRowMask != revRowMask {
return -1
}
if curColMask != colMask && curColMask != revColMask {
return -1
}
if curRowMask == rowMask {
sameRow++
}
if curColMask == colMask {
sameCol++
}
}
f := func(mask, cnt int) int {
ones := bits.OnesCount(uint(mask))
if n%2 == 1 {
if abs(n-ones*2) != 1 || abs(n-cnt*2) != 1 {
return -1
}
if ones == n/2 {
return n/2 - bits.OnesCount(uint(mask&0xAAAAAAAA))
}
return (n+1)/2 - bits.OnesCount(uint(mask&0x55555555))
} else {
if ones != n/2 || cnt != n/2 {
return -1
}
cnt0 := n/2 - bits.OnesCount(uint(mask&0xAAAAAAAA))
cnt1 := n/2 - bits.OnesCount(uint(mask&0x55555555))
return min(cnt0, cnt1)
}
}
t1 := f(rowMask, sameRow)
t2 := f(colMask, sameCol)
if t1 == -1 || t2 == -1 {
return -1
}
return t1 + t2
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}