标签:sg hdu 博弈
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3980
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
Sample Output
Case #1: aekdycoin
Case #2: abcdxyzk
PS:http://blog.csdn.net/zhjchengfeng5/article/details/8214768
题意
两个人在一个由 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了。。。
代码如下:
#include <cstdio>
#include <cstring>
int SG[1056];
int vis[1056];
int n, m;
int get_SG()
{
memset(SG,0,sizeof(SG));
SG[m] = 1;
for(int i = m+1; i <= n; i++)//get Sprague-Grundy value;
{
memset(vis,0,sizeof(vis));
for(int j = 0; j < i-m; j++)
{
vis[SG[j]^SG[i-m-j]] = 1;
}
for(int j = 0; ; j++)//求mes{}中未出现的最小的非负整数
{
if(!vis[j])
{
SG[i] = j;
break;
}
}
}
return SG[n-m];
}
int main ()
{
int t;
int cas = 0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
printf("Case #%d: ",++cas);
if(get_SG() || n < m)
printf("abcdxyzk\n");
else
printf("aekdycoin\n");
}
return 0;
}
HDU 3980 Paint Chain(博弈 SG)
标签:sg hdu 博弈
原文地址:http://blog.csdn.net/u012860063/article/details/43496903