HDU 5724 – Chess [SG函数+状压]

Link

http://acm.hdu.edu.cn/showproblem.php?pid=5724

Problem

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.

Mean

有一个N行20列的棋盘。两个人玩游戏,可以移动其中一行的一个棋子,移动到他右边第一个空位。两个人轮流移动,直到不能移动就输掉了。

Analysis

每一行的情况能状态压缩成一个20位的int,求它的sg函数,最后每一行异或一下就好。

会SG函数的一般不成问题。

Code

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1<<20;
int sg[maxn+1024], vis[25];

void getSG() {
    for(int s = 1; s < maxn; ++s) {
        memset(vis, false, sizeof(vis));
        for(int i = 20; i >= 0; --i) {
            if(s & (1<<i)) {
                for(int j = i-1; j >= 0; --j) {
                    if(!(s & (1<<j))) {
                        vis[sg[s ^ (1<<i) ^ (1<<j)]] = true;
                        break;
                    }
                }
            }
        }
        for(int j = 0; ; ++j) {
            if(!vis[j]) {
                sg[s] = j;
                break;
            }
        }
    }
}


int main() {
    getSG();
    int T; scanf("%d", &T);
    while(T--) {
        int N; scanf("%d", &N);
        int ans = 0;
        while(N--) {
            int cur = 0;
            int M; scanf("%d", &M);
            while(M--) {
                int x; scanf("%d", &x);
                cur = cur | (1<<(20-x));
            }
            ans ^= sg[cur];
        }
        if(ans) puts("YES");
        else puts("NO");
    }
    return 0;
}

欢迎留言