hdu 5724-Chess(状态压缩+sg函数)

本文提供了一种解决HDU5724-Chess问题的方法,通过运用Sprague-Grundy定理计算博弈论问题中的SG函数值,实现了一个高效的算法解决方案。

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

hdu 5724-Chess

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=20;
int SG[1<<N];
bool S[N];
void get_SG(int n)
{
    memset(SG,0,sizeof(SG));
    for(int i=0;i<n;i++)
    {
        memset(S,false,sizeof(S));
        for(int j=29;j>=0;j--)
        {
            if(i&(1<<j))
            {
                for(int k=j-1;k>=0;k--)
                {
                    if(!(i&(1<<k)))
                    {
                        int temp=i;
                        temp^=(1<<j)^(1<<k);
                        S[SG[temp]]=true;
                        break;
                    }
                }
            }
        }
        for(int j=0;j<N;j++)
        if(!S[j])
        {
            SG[i]=j;
            break;
        }
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int t;
    get_SG(1<<20);
    scanf("%d",&t);
    while(t--)
    {
        int n,m,a,ans=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d",&m);
            int temp=0;
            while(m--)
            {
                scanf("%d",&a);
                temp^=(1<<(20-a));
            }
            ans^=SG[temp];
        }
        if(ans)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/widsom/p/7170891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值