Problem Description:
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.
大意:题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜。
题解:组合博弈问题,应用sg函数+状态压缩dp可解。
sg函数可看白书的组合游戏部分。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = (1 << 20) + 10;
int SG[maxn];
int cnt = 0;
int n,m;
int dfs(int k)
{
if(SG[k]!=-1) return SG[k];
int vis[30];
memset(vis,0,sizeof(vis));
for(int i=0;i<19;i++){
int temp = k;
if(k & (1 << i) == 0) continue;
if((k & (1 << i))) {
for(int j=i+1;j<=19;j++){
if((k & (1 << j)) == 0) {
temp = k;
temp ^= (1 << i);
temp ^= (1 << j);
vis[dfs(temp)] = 1;
break;
}
}
}
}
for(int i = 0;i < 29;i++)
if(vis[i]==0) {
SG[k] = i;
return i;
}
}
int main()
{
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
memset(SG,-1,sizeof(SG));
int temp = 0;
for(int i=19;i>=0;i--){
temp += (1 << i);
SG[temp] = 0;
}
int kase;scanf("%d",&kase);
while(kase--){
int temp = 0,a,ans = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
temp = 0;
scanf("%d",&m);
for(int j=1;j<=m;j++){
scanf("%d",&a);
temp ^= (1 << (a-1));
}
int flag = dfs(temp);
ans ^= SG[temp];
}
if(ans != 0) printf("YES\n");
else printf("NO\n");
}
return 0;
}