您的位置:首页 > 其它

hdu5724Chess+SG博弈

2016-07-22 20:35 337 查看
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.

Input

Multiple test cases.

The first line contains an integer T(T≤100), indicates the number of test cases.

For each test case, the first line contains a single integer n(n≤1000), the number of lines of chessboard.

Then n lines, the first integer of ith line is m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20) followed, the position of each chess.

Output

For each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.

Sample Input

2

1

2 19 20

2

1 19

1 18

Sample Output

NO

YES

Author

HIT

Source

2016 Multi-University Training Contest 1

//第一次跟着大大们做多校,乱写一通代码,wa着玩。赛后补。。

给一个n*20的棋盘,先给出了一堆棋子的位置,两个人轮流着进行操作。

操作:可以将棋子移到右边的第一个空位上,(不能多隔空位)

11100之后的状态可以有。11010,10110,01110.

假如直接将这种状态看成一个数(20位,不大),所以直接状压预处理出所有状态的SG,然后N行直接多问题SG亦或。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool SG[2000004];
int a[24];
bool visit[24];

int pow2[22];

inline void getp(){
pow2[0]=1;
pow2[1]=2;
for(int i=2;i<=20;i++) pow2[i]=pow2[i-1]*2;
}
inline void geta(int x){
int cnt=20;
memset(a,0,sizeof(a));
while(x){
a[cnt--]=x%2;
x=x/2;
}
}
inline int getnew(int x,int i,int j){
return x-pow2[i-1]+pow2[j-1];
}

void getSG(){
memset(SG,0,sizeof(SG));
int zhuang=(1<<20)-1;
for(int i=1;i<=zhuang;i++){
geta(i);
memset(visit,0,sizeof(visit));
int k=21;
for(int j=20;j>=1;j--){
if(a[j]==0){
k=j;
continue;
}
if(k==21) continue;
visit[SG[getnew(i,21-j,21-k)]]=true;
}
for(int j=0;;j++){
if(visit[j]==false){
SG[i]=j;
break;
}
}
}
}
int main(){
getp();
getSG();
int t;
scanf("%d",&t);
while(t--){
int n;
int out=0;
scanf("%d",&n);
while(n--){
int pp=0,w,c;
scanf("%d",&c);
while(c--){
scanf("%d",&w);
pp+=pow2[20-w];
}
out^=SG[pp];
}
if(out) printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: