HDU 5742 Chess SG函数博弈

Chess

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
 
2 1 2 19 20 2 1 19 1 18
 
Sample Output
 
NO YES
 
 
题意:
  给你一个N*20的棋盘
  每一行有若干棋子,A,B轮流移动棋子,每次可以选定一个棋子移动到右边第一个空格,直到不能移动的人败
题解:
  SG
  吧SG(1<<20)的状态全部预处理出来就可以了
  再把sg值全部异或起来,为0先手输
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+10, M = 2e5+20, mod = 1e9+7, inf = 2e9;

int sg[1<<22],vis[30];
void init() {
    int NN = 1<<20;
    for(int i = NN; i >= 0; --i) {
        memset(vis,0,sizeof(vis));
        int last = -1;
        for(int j = 19; j >= 0; --j) {
            if((i&(1<<j)) == 0) last = j;
            else  {
                if(last == -1) continue;
                int now = i - (1<<j) + (1<<last);
                vis[sg[now]] = 1;
            }
        }
        for(int j = 0; j <= 25; ++j) {
                if(!vis[j]) {
                    sg[i] = j;
                    break;
                }
        }
    }
}
int main() {
        init();
        int T,m,x,y;
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&m);
            int ans = 0;
            for(int i = 1; i <= m; ++i) {
                int now = 0;
                scanf("%d",&x);
                while(x--) {
                    scanf("%d",&y);
                    y--;
                    now += (1<<(y));
                }
                ans ^= sg[now];
            }
            if(ans) puts("YES");
            else puts("NO");
        }
        return 0;
}
原文地址:https://www.cnblogs.com/zxhl/p/6011855.html