#### HDU 5724 – Chess [SG函数+状压]

http://acm.hdu.edu.cn/showproblem.php?pid=5724

### Problem

Alice and Bob are playing a special chess game on an n × 20 chessboard. There are several chesses on the chessboard. They can move one chess in one turn. If there are no other chesses on the right adjacent block of the moved chess, move the chess to its right adjacent block. Otherwise, skip over these chesses and move to the right adjacent block of them. Two chesses can’t be placed at one block and no chess can be placed out of the chessboard. When someone can’t move any chess during his/her turn, he/she will lose the game. Alice always take the first turn. Both Alice and Bob will play the game with the best strategy. Alice wants to know if she can win the game.

### Code

```#include <bits/stdc++.h>
using namespace std;

const int maxn = 1<<20;
int sg[maxn+1024], vis[25];

void getSG() {
for(int s = 1; s < maxn; ++s) {
memset(vis, false, sizeof(vis));
for(int i = 20; i >= 0; --i) {
if(s & (1<<i)) {
for(int j = i-1; j >= 0; --j) {
if(!(s & (1<<j))) {
vis[sg[s ^ (1<<i) ^ (1<<j)]] = true;
break;
}
}
}
}
for(int j = 0; ; ++j) {
if(!vis[j]) {
sg[s] = j;
break;
}
}
}
}

int main() {
getSG();
int T; scanf("%d", &T);
while(T--) {
int N; scanf("%d", &N);
int ans = 0;
while(N--) {
int cur = 0;
int M; scanf("%d", &M);
while(M--) {
int x; scanf("%d", &x);
cur = cur | (1<<(20-x));
}
ans ^= sg[cur];
}
if(ans) puts("YES");
else puts("NO");
}
return 0;
}
```