Chess HDU - 5724 (博弈)(sg函数)

本文介绍了一种特殊规则的棋盘游戏,并通过使用二进制表示与SG函数计算来解决游戏中玩家胜败的问题。通过对棋盘状态的数学抽象,采用数组实现计算并预处理所有可能的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

Then nn lines, the first integer of ith line is m(m≤20)m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20)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

这题后来想到才20个,感觉可以用二进制表示提前处理一遍,其实问题的关键这么处理计算sg函数,因为之前计算sg的时候用的都是set,可是发现超时啊。。。,后来我才开始考虑一个问题,一个sg函数的可能取值是多少,实在没办法,打了一个10000内的表观察了一下,发现似乎不是很大,于是打算用数组实现一波,但是数组开多大很纠结,用了30发现居然过了。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<cstring>
#define INF 0x7fffffff
using namespace std;
int sg[1<<20];

int main()
{
    sg[0]=0;
    sg[1]=0;//初始化
    bool vis[30];
    for(int s=2;s<(1<<20);s++)
    {
        memset(vis,false,sizeof(vis));
        for(int i=0;(1<<i)<s;i++)
        {
            if(!(s&(1<<i)))//找到一个为0的位置
            {
                int j=i+1;
                while(s&(1<<j))//那么只要在这个位置前面为1的值都可以移动
                {
                    vis[sg[s^(1<<j)^(1<<i)]]=true;
                    j++;
                }
            }
        }
        int i=0;
        while(vis[i])i++;
        sg[s]=i;//返回sg就好
    }
    int t;
    scanf("%d",&t);
    int n;
    int m;
    int x;
    /*for(int i=0;i<10000;i++)
        cout<<i<<":"<<sg[i]<<endl;*/
    while(t--)
    {
        scanf("%d",&n);
        int ans=0;
        while(n--)
        {
            scanf("%d",&m);
            int num=0;
            while(m--)
            {
                scanf("%d",&x);
                num+=1<<(20-x);
            }

            ans^=sg[num];
        }
        //cout<<ans<<endl;
        if(!ans)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值