HDU 2999 Stone Game, Why are you always there?

本文解析了一道经典的博弈论题目,通过使用SG函数解决了一个关于取石子游戏的问题。玩家从一排编号为1到n的石子中取走连续的石子,数量必须是给定集合中的数。文章提供了详细的解题思路及C++代码实现。

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2999


题意:给你n个数的集合,表示你每次取石子只能为集合里的数,然后给你一排石子,编号为1~n,每次你可以取相邻位置的连续石子(数量只能为集合里的数),注意石子的位置时不变的,比如把2拿走了,1和3还是不相邻的。问先手有没有机会赢。


思路:如果我们取靠边的x个石子那么就是转移成sg[i-x],如果我们取中间的石子,就变成的不相邻的两排,也就是把单一的游戏拆成了两个,这样想就简单了。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

int n,m;
int a[109];
int sg[1009];

void init()
{
    bool temp[1000];
    rep(i,1,n) scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    sg[0] = 0;
    rep(i,1,1000)
    {
        Clean(temp,false);
        rep(j,1,n) //枚举取的个数
        {
            if ( a[j] > i ) break;
            temp[ sg[i-a[j]] ] = true; //取边上的
            if ( i - a[j] > 1 )
            {
                int tot = i - a[j];
                rep(k,1,tot-1) //枚举断开位置,变成两堆石子
                    temp[ sg[k] ^ sg[tot-k] ] = true;
            }
        }
        int j = 0;
        while( temp[j] ) j++;
        sg[i] = j;
    }
}

int main()
{
    while( cin>>n )
    {
        int temp;
        init();
        scanf("%d",&m);
        rep(i,1,m)
        {
            scanf("%d",&temp);
            puts( sg[temp] ? "1" : "2" );
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值