zoukankan      html  css  js  c++  java
  • HDU5724 Chess(SG定理)

    题目

    Source

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

    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

    分析

    题目大概说有n行,每行20格子,都有一些棋子,两个人轮流进行这个操作:选择某一行一个棋子移动到该行右边第一个空的格子。不能进行的人输。问先手是否能赢。

    这个博弈显然是组合博弈,先手后手交替进行、每次决策是有限的、有胜利条件。。然后SG定理搞了。。求SG值要注意时间复杂度。

    代码

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int sg[1<<20];
    int main(){
        for(int s=0; s<(1<<20); ++s){
            int k=100;
            bool vis[21]={0};
            for(int i=19; i>=0; --i){
                if((s>>i&1)==0) continue;
                if(k<=i-1){
                    vis[sg[(s^(1<<i))^(1<<k)]]=1;
                }else{
                    for(int j=i-1; j>=0; --j){
                        if((s>>j&1)==0){
                            k=j;
                            vis[sg[(s^(1<<i))^(1<<k)]]=1;
                            break;
                        }
                    }
                }
            }
            for(int i=0; i<=20; ++i){
                if(!vis[i]){
                    sg[s]=i;
                    break;
                }
            }
        }
    
        int t,n,m,a;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            int res=0;
            for(int i=0; i<n; ++i){
                scanf("%d",&m);
                int s=0;
                while(m--){
                    scanf("%d",&a);
                    s|=1<<20-a;
                }
                res^=sg[s];
            }
            if(res) puts("YES");
            else puts("NO");
        }
        return 0;
    }
    
  • 相关阅读:
    Prim算法以及Kruskal算法
    PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
    PAT甲级考前整理(2019年3月备考)之一
    PAT甲级考前整理(2019年3月备考)之二,持续更新中.....
    linux下挂载U盘
    opencv2已有的情况下,安装opencv3以及对应的opencv_contrib
    二维数组和二级指针做函数参数的问题
    ubuntu14.04下 python2.7怎么链接到安装在指定文件夹的opencv3
    使用opencv的nonfree模块
    PaddlePaddle开源平台的应用
  • 原文地址:https://www.cnblogs.com/WABoss/p/5687065.html
Copyright © 2011-2022 走看看