-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
44 lines (43 loc) · 1.63 KB
/
Solution.cpp
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
class Solution {
public:
int n;
int movesToChessboard(vector<vector<int>>& board) {
n = board.size();
int mask = (1 << n) - 1;
int rowMask = 0, colMask = 0;
for (int i = 0; i < n; ++i) {
rowMask |= board[0][i] << i;
colMask |= board[i][0] << i;
}
int revRowMask = mask ^ rowMask;
int revColMask = mask ^ colMask;
int sameRow = 0, sameCol = 0;
for (int i = 0; i < n; ++i) {
int curRowMask = 0, curColMask = 0;
for (int 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;
sameRow += curRowMask == rowMask;
sameCol += curColMask == colMask;
}
int t1 = f(rowMask, sameRow);
int t2 = f(colMask, sameCol);
return t1 == -1 || t2 == -1 ? -1 : t1 + t2;
}
int f(int mask, int cnt) {
int ones = __builtin_popcount(mask);
if (n & 1) {
if (abs(n - ones * 2) != 1 || abs(n - cnt * 2) != 1) return -1;
if (ones == n / 2) return n / 2 - __builtin_popcount(mask & 0xAAAAAAAA);
return (n + 1) / 2 - __builtin_popcount(mask & 0x55555555);
} else {
if (ones != n / 2 || cnt != n / 2) return -1;
int cnt0 = (n / 2 - __builtin_popcount(mask & 0xAAAAAAAA));
int cnt1 = (n / 2 - __builtin_popcount(mask & 0x55555555));
return min(cnt0, cnt1);
}
}
};