hdu3980Paint Chain+SG博弈

本文介绍了一种基于游戏策略的分析方法,通过使用Sprague-Grundy定理解决了一类特殊的涂色游戏问题。游戏设定为玩家轮流给圆环上的珠子涂色,每次涂色必须选择连续的一段珠子进行,直至无法涂色者失败。文章详细解析了如何通过记忆化递归计算游戏的状态,并给出了完整的代码实现。

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

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

Author
jayi

Source
2011 Multi-University Training Contest 14 - Host by FZU

一个圆环N个数,每次每个人只能给连续的M个数抹漆,最后不能抹的就算输(大概这个意思啦,英语不好)。。首先第一个人抹了之后就变成直线了。。先手变后手了..
(5,2) +++++之后的状态可以有<–+++><+–++><++–+><+++–>
一个问题的结果是其子问题的结果的亦或。。。
对于一个直线的情况,其子状态有前边空几个那么多的情况。。
比如(5,2) +++++,
当前边空0个时子游戏为(0,2)(3,2)
当前边空1个时子游戏有(1,2)(2,2)
当前边空2个时子游戏有(2,2)(1,2)
当前边空3个时子游戏有(3,2)(0,2)
所以求SG时记忆化递推。。就可以求出(n,m)状态的SG。

好了贴代码。。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long LL;
int SG[1005];

int getSG(int n,int m){
    if(n<m) return SG[n]=0;
    if(SG[n]!=-1) return SG[n];
    bool visit[1005];// //下边递归了。。不要放全局去。。。

    memset(visit,false,sizeof(visit));
    for(int i=0;i<=n-m;i++){
        visit[getSG(i,m)^getSG(n-m-i,m)]=true;
    }
    for(int i=0;;i++){
        if(visit[i]==false){
            SG[n]=i;
            break;
        }
    }
    return SG[n];
}
int main(){
    int t;
    scanf("%d",&t);
    for(int test=1;test<=t;test++){
        int n,m;
        scanf("%d %d",&n,&m);
        memset(SG,-1,sizeof(SG));
        printf("Case #%d: ",test);
        if(n<m||getSG(n-m,m)) printf("abcdxyzk\n");
        else printf("aekdycoin\n");
    }
    return 0;
}

/*
2
3 1
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值