hdu 3980 Paint Chain 博弈+SG函数 每次选连续M个

游戏策略博弈分析
本文介绍了一种基于博弈论的游戏策略分析方法,通过编程实现游戏中的最优策略选择过程,并以两个玩家轮流涂色珠子的游戏为例,展示了如何通过计算SG函数来判断胜者。
Problem Description
Aekdycoin and abcdxyzk are playing a game. They get a circle chain with some beads. Initially none of the beads is painted. They take turns to paint the chain. In Each turn one player must paint a unpainted beads. Whoever is unable to paint in his turn lose the game. Aekdycoin will take the first move.

Now, they thought this game is too simple, and they want to change some rules. In each turn one player must select a certain number of consecutive unpainted beads to paint. The other rules is The same as the original. Who will win under the rules ?You may assume that both of them are so clever.
 


 

Input
First line contains T, the number of test cases. Following T line contain 2 integer N, M, indicate the chain has N beads, and each turn one player must paint M consecutive beads. (1 <= N, M <= 1000)
 


 

Output
For each case, print "Case #idx: " first where idx is the case number start from 1, and the name of the winner.
 


 

Sample Input
2 3 1 4 2
 


 

Sample Output
Case #1: aekdycoin Case #2: abcdxyzk

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1100;
//一条线  每次取连续M个 SG函数
int sg[maxn],flag[maxn];
int calc_sg(int n,int m)
{
    for(int i=0;i<m;i++) sg[i]=0;
    for(int i=m;i<=n;i++)
    {
        memset(flag,0,sizeof(flag));
        for(int j=0;j+m<=i;j++)
        {
            flag[sg[j]^sg[i-j-m]]=1;
        }
        for(int j=0;;j++)
        {
            if(flag[j]==0)
            {
                sg[i]=j;break;
            }
        }
    }
}
int main()
{
    int ci,pl=1;scanf("%d",&ci);
    while(ci--)
    {
        int n,m;scanf("%d%d",&n,&m);
        if(n<m)
        {
            printf("Case #%d: abcdxyzk\n",pl++);
            continue;
        }
        calc_sg(n-m,m);
        if(sg[n-m]==0) printf("Case #%d: aekdycoin\n",pl++);
        else printf("Case #%d: abcdxyzk\n",pl++);
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值