[原创]hdu 5724 Chess [状压+SG函数]【博弈】

2017-01-24 13:51:16 Tabris_ 阅读数:283


博客爬取于2020-06-14 22:41:58
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54707350


题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5724
-----------------------------------------------------------------------------------.
Chess

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2320 Accepted Submission(s): 985

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

-----------------------------------------------------------------------------------.

题目大意:
就是有n个一维的棋盘,两个人轮流走,每次可以将一个棋子移到距离它最近的右边的空位上。最后谁不能移动棋子了就输了。

解题思路
题目中给定的每一维的状态可以用长度为20的二进制数表示,1为有棋子,0为没有棋子。
表示出来之后可以转移出每种状态的后继状态求出对应的SG值,最后将N个SG值异或一下就好了。

在每一次移动的时候对应的二进制数都会变小,所以从1->(1<<20)预处理一下结果就好了.

开始写的记忆化搜索的形式怎么都是WA,后来改了一发预处理就AC了.

附本题代码
-----------------------------------------------------------------------------------.

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
# include <bits/stdc++.h>
using namespace std;
void display_bit(int a){
for(int i=31;i>=0;i--){
if(a&(1<<i)) printf("1");
else printf("0");
}
printf(" -- %d\n",a);
}
/***********************************************************************/
int sg[(1<<20)+100],tmp,p;bool h[30];
void init(){
sg[0]=0;
for(int i=1;i<(1<<20);i++){
memset(h,false,sizeof(h));
p=-1;
for(int j=0;j<20;j++){
if((i&(1<<j))==0) p=j;
else if(p!=-1){
tmp = i^(1<<j)^(1<<p);
if(sg[tmp]<30) h[sg[tmp]]=true;
}
}
for(int j=0;;j++) if(!h[j]) {sg[i]=j;break; }
}
}
int main(){
init();
int _;
while(~scanf("%d",&_)){
while(_--){
int n;
scanf("%d",&n);
int ans = 0,x,y,tem;
for(int i=0;i<n;i++){
scanf("%d",&x);
tem = 0;
for(int j=0;j<x;j++){
scanf("%d",&y);
tem |= (1<<(20-y));
}
ans^=sg[tem];
}
if(ans) puts("YES");
else puts("NO");
}
}
return 0;
}