hdu 5724 Chess (SG函数)

来源:互联网 发布:江苏海纳船务知乎 编辑:程序博客网 时间:2024/05/16 01:12

Chess

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


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(T100), indicates the number of test cases.

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

Then n lines, the first integer of ith line is m(m20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1pj20)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
212 19 2021 191 18
 

Sample Output
NOYES
 

题意:有一个棋盘,每行一些位置有棋子。右边无棋子时棋子可以向右移动,有棋子的可以跳到他后面第一个空位。不能出棋盘,不能操作的算输。问先手是否能赢。


解法是SG函数。每一行看作一种状态,因为一行最多只有20个棋子,把有棋子的地方设为1,没有棋子的地方设为0,构成一个二进制数,转换为十进制数作为一种状态。根据题意可以找到它可以由哪些状态转移而来,根据这个就可以生成SG函数映射的每一个值。

CODE

#include <bits/stdc++.h>using namespace std;const int N = (1<<20)+10;int SG[N];int sg(int x){    if(SG[x] != -1) return SG[x];    bool vis[500] = {false};    for(int i = 0;i <= 19;i++){        if((1<<i)&x){            int tmp = x;            for(int j = i-1;j >= 0;j--){///可以由右边第一个非0位置进行转移                if(((1<<j)&x) == 0){                    tmp ^= ((1<<i)^(1<<j));                    vis[sg(tmp)] = true;                    break;                }            }        }    }    for(int i = 0;;i++)        if(!vis[i]) return SG[x] = i;}int main(void){    int T;    scanf("%d",&T);    memset(SG,-1,sizeof SG);    SG[1] = 0;    for(int i = 0;i <= (1<<20);i++) SG[i] = sg(i);    while(T--){        int n;        scanf("%d",&n);        int ans = 0;        for(int i = 1;i <= n;i++){            int m;            scanf("%d",&m);            int tmp = 0;            for(int j = 1;j <= m;j++){                int t;                scanf("%d",&t);                tmp ^= (1<<(20-t));  ///因为是反过来的,所以需要翻转            }            ans ^= SG[tmp];        }        if(ans) puts("YES");        else    puts("NO");    }    return 0;}



0 0
原创粉丝点击