首页 > ACM题库 > HDU-杭电 > HDU 3980-Paint Chain-博弈论-[解题报告]HOJ
2015
04-14

HDU 3980-Paint Chain-博弈论-[解题报告]HOJ

Paint Chain

问题描述 :

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.

输入:

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)

输出:

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)

样例输入:

2 
3 1 
4 2

样例输出:

Case #1: aekdycoin 
Case #2: abcdxyzk

题意

    两个人在一个由 n 个玻璃珠组成的一个圆环上玩涂色游戏,游戏的规则是:
        1、每人一轮,每轮选择一个长度为 m 的连续的、没有涂过色的玻璃珠串涂色
        2、不能涂色的那个人输掉游戏

做法分析

    算是比较裸的 SG 函数的应用吧,主要是刚才突然对
博弈 突然来兴趣了,于是就捡了篇论文看看,然后百度了一道水题练练手。。。

    可以肯定的是,第一个人涂色之后就把环变成了一个长度为
n-m 的链了,那么我们就可以这样划分阶段了:每轮从一些线段中选择一个,并且把那条线段分成 x, m, len-x-m 的三个部分,其中 len 表示线段原来的长度,m 表示的是已经涂色了,那么这个长为 len 的线段能够得到的子状态最多有 len-m+1 个(其实由对称性可知实际的状态数只是这里的一半),变成了两个长度为 x 和 len-x-m 子游戏,由 SG 定理,SG(len)=SG(x)^SG(len-x-m) 而再由 SG 函数的定义式 SG[u]=mex(seg[v]) 其中,状态 u 可以直接得到状态
v,再加上一个记忆化的小优化,AC了。。。

    过几天再总结总结 SG
函数和 SG 定理

AC通道

参考代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;

int t, n, m;
int f[1005];
bool emerge[1000];

int get_sg(int len)
{
    if(len<m) return f[len]=0;
    if(f[len]!=-1) return f[len];
    bool vs[1001]={0};
    for(int i=0; len-i-m>=0; i++)
        vs[get_sg(i)^get_sg(len-i-m)]=1;
    for(int i=0; i<1001; i++)
        if(!vs[i]) return f[len]=i;
}

int main()
{
    scanf("%d", &t);
    for(int ca=1; ca<=t; ca++)
    {
        scanf("%d%d", &n, &m);
        memset(f, -1, sizeof(f));
        printf("Case #%d: ", ca);
        if(n<m || get_sg(n-m)) printf("abcdxyzk\n");
        else printf("aekdycoin\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/zhjchengfeng5/article/details/8214768


  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.