博弈训练——sg函数
来源:互联网 发布:刚买的域名被墙 编辑:程序博客网 时间:2024/06/06 14:16
关于sg函数:
———————————————————————
转自:http://blog.csdn.net/luomingjun12315/article/details/45555495
Sprague-Grundy定理(SG定理):
游戏和的SG函数等于各个游戏SG函数的Nim和。这样就可以将每一个子游戏分而治之,从而简化了问题。而Bouton定理就是Sprague-Grundy定理在Nim游戏中的直接应用,因为单堆的Nim游戏 SG函数满足 SG(x) = x。
SG函数:
首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。
对于任意状态 x , 定义 SG(x) = mex(S),其中 S 是 x 后继状态的SG函数值的集合。如 x 有三个后继状态分别为 SG(a),SG(b),SG(c),那么SG(x) = mex{SG(a),SG(b),SG(c)}。 这样 集合S 的终态必然是空集,所以SG函数的终态为 SG(x) = 0,当且仅当 x 为必败点P时。
【实例】取石子问题
有1堆n个的石子,每次只能取{ 1, 3, 4 }个石子,先取完石子者胜利,那么各个数的SG值为多少?
SG[0]=0,f[]={1,3,4},
x=1 时,可以取走1 - f{1}个石子,剩余{0}个,所以 SG[1] = mex{ SG[0] }= mex{0} = 1;
x=2 时,可以取走2 - f{1}个石子,剩余{1}个,所以 SG[2] = mex{ SG[1] }= mex{1} = 0;
x=3 时,可以取走3 - f{1,3}个石子,剩余{2,0}个,所以 SG[3] = mex{SG[2],SG[0]} = mex{0,0} =1;
x=4 时,可以取走4- f{1,3,4}个石子,剩余{3,1,0}个,所以 SG[4] = mex{SG[3],SG[1],SG[0]} = mex{1,1,0} = 2;
x=5 时,可以取走5 - f{1,3,4}个石子,剩余{4,2,1}个,所以SG[5] = mex{SG[4],SG[2],SG[1]} =mex{2,0,1} = 3;
以此类推…..
x 0 1 2 3 4 5 6 7 8….
SG[x] 0 1 0 1 2 3 2 0 1….
———————————————————————–
参考模板:http://blog.csdn.net/y990041769/article/details/21406335
int sg[N];bool hash[N];void sg_solve(int *s,int t,int N) //N求解范围 S[]数组是可以每次取的值,t是s的长度。{ int i,j; memset(sg,0,sizeof(sg)); for(i=1;i<=N;i++) { memset(hash,0,sizeof(hash)); for(j=0;j<t;j++) if(i - s[j] >= 0) hash[sg[i-s[j]]] = 1; for(j=0;j<=N;j++) if(!hash[j]) break; sg[i] = j; }}
————————————————————————–
其他:
sg函数的值,个人理解是i结点对应的必败点(错误的理解)
经验证sg函数的最大值和set[]的元素个数密切相关,最大就是n
训练5题:
lightoj 1296 Again Stone Game
hdu 1848 Fibonacci again and again
hdu 1536 S-Nim
LightOJ 1315 Game of Hyper Knights
lightOJ 1199 Partitioning Game
lightoj 1296 Again Stone Game(sg博弈)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1296
大意:n堆石子,每次可以从其中一堆取出1-k个,k是不大于该堆石子个数一半的数字。不能取的人输。求解两人博弈结果。
分析:
每一堆石子的个数可达到1e9。计算每一个sg函数存储下来肯定不行,所有打表找找sg结果的规律。
打表sg[i]:
sg[0]=0; sg[1]=0; for(int i=2;i<=15;i++){ int sta[20],top=0,L=i>>1; for(int j=1;j<=L;j++){ sta[top++]=sg[i-j]; } for(int j=0;j<=i;j++) { bool tag=0; for(int k=0;k<top;k++) if(j==sta[k]){ tag=1; break; } if(tag==0) { sg[i]=j; break; } } }
可以发现当i是偶数sg[i]=i/2, 当i是奇数时,sg[i]=sg[i/2]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N=1e3+10; int calc(int n){ if((n&1)==0) return n/2; return calc(n/2); } int main() { //freopen("cin.txt","r",stdin); int t,n,a,ca=1; cin>>t; while(t--){ scanf("%d",&n); int ans=0,a; for(int i=0;i<n;i++){ scanf("%d",&a); ans=ans^calc(a); } if(ans)printf("Case %d: Alice\n",ca++); else printf("Case %d: Bob\n",ca++); } return 0; }
hdu 1848 Fibonacci again and again(sg)
http://acm.hdu.edu.cn/showproblem.php?pid=1848
大意:3堆石子每次取石子的个数是菲波那切数列元素,最后取完所有石子的人为赢家。
分析:sg函数的简单应用
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;int s[1005],len;int sg[1005];bool tag[1005];void calc_sg(){ int i,j; s[0]=1; s[1]=2; for(i=2;i<1005;i++) { s[i]=s[i-2]+s[i-1]; if(s[i]>1000) { len=i; break; } } for(i=1;i<=1000;i++){ memset(tag,0,sizeof(tag)); for(j=0;j<len;j++){ if(i<s[j]) break; tag[sg[i-s[j]]]=1; } for(j=0;j<=1000;j++) if(tag[j]==0) break; sg[i]=j; }}int main(){ calc_sg(); int m,n,p; while(cin>>m>>n>>p){ if(m+n+p==0) break; int ans=sg[m]^sg[n]^sg[p]; if(ans) puts("Fibo"); else puts("Nacci"); } return 0;}
hdu 1536 S-Nim (sg)
http://acm.hdu.edu.cn/showproblem.php?pid=1536
背景故事——nim游戏:
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:
The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.
The players take turns chosing a heap and removing a positive number of beads from it.
The first player not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:
Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).
If the xor-sum is 0, too bad, you will lose.
Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
The player that takes the last bead wins.
After the winning player’s last move the xor-sum will be 0.
The xor-sum will change after every move.
现在每一次不能取非0任意数,求解胜利和失败的情况判别:
标准SG函数的应用。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N=1e4+10;int s[105];int sg[N];bool tag[N];void calc_sg(int n){ memset(sg,0,sizeof(sg)); int i,j; for(i=1;i<N;i++){ memset(tag,0,sizeof(tag)); for(j=0;j<n;j++) if(i-s[j]>=0) tag[sg[i-s[j]]]=1; for(j=0;j<N;j++) if(tag[j]==0) break; sg[i]=j; }}int main(){ //freopen("cin.txt","r",stdin); int n; while(cin>>n&&n){ for(int i=0;i<n;i++){ scanf("%d",&s[i]); } calc_sg(n); int t,m,num,ans; scanf("%d",&t); for(int i=0;i<t;i++){ scanf("%d",&m); ans=0; for(int j=0;j<m;j++){ scanf("%d",&num); ans=ans^sg[num]; } if(ans)printf("%c",'W'); else printf("%c",'L'); } puts(""); } return 0;}
LightOJ 1315 Game of Hyper Knights(sg博弈)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1315
大意:在如图的方格中进行两人博弈,棋子的走法有6种,不能走的人算输。
分析:递归类博弈。我只能说这题很神奇,我下面的tag数组设成全局的死活过不了,设成局部瞬间过了。因为棋子的走法只有6种,所以sg函数只有6种值0-5
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N=1005,L=10;int xx[6]={-2,-3,-2,-1,-1,1};int yy[6]={1,-1,-1,-2,-3,-2};int sg[N][N];bool vis[N][N];//int sta[L],top;int calc_sg(int x,int y){ if(vis[x][y]) return sg[x][y]; bool tag[L]; memset(tag,0,sizeof(tag)); for(int i=0;i<L;i++) tag[i]=0; for(int i=0;i<6;i++){ int tx=x+xx[i]; int ty=y+yy[i]; if(tx>=0 && ty>=0) tag[calc_sg(tx,ty)]=1; //sta[top++]=calc_sg(tx,ty); } vis[x][y]=1; for(int i=0;i<L;i++){ if(tag[i]==0){ sg[x][y]=i; break; } } return sg[x][y];}int main(){ //freopen("cin.txt","r",stdin); int t,ca=1; int n; cin>>t; while(t--){ scanf("%d",&n); int ans=0; for(int i=0;i<n;i++){ int a,b; scanf("%d%d",&a,&b); ans=ans^calc_sg(a,b); } printf("Case %d: ",ca++); if(ans) puts("Alice"); else puts("Bob"); } return 0;}
lightOJ 1199 Partitioning Game(sg博弈)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1199
大意:给出N个堆,针对每个堆每一次取1-k,k小于N的一半,不能取的人失败。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N=1e4+10;int sg[N];bool vis[N];void init(int n){ sg[1]=sg[2]=0; sg[3]=1; sg[4]=0; for(int i=5;i<n;i++){ memset(vis,0,sizeof(vis)); for(int j=1;2*j<i;j++) vis[sg[j]^sg[i-j]]=1; int j=0; while(vis[j]) j++; //能有效分割的次数 sg[i]=j; }}int main(){ //freopen("cin.txt","r",stdin); init(N); int t,n,ca=1; cin>>t; while(t--){ scanf("%d",&n); int ans=0,a; for(int i=0;i<n;i++){ scanf("%d",&a); ans=ans^sg[a]; } if(ans) printf("Case %d: Alice\n",ca++); else printf("Case %d: Bob\n",ca++); } return 0;}
- 博弈训练——sg函数
- 博弈——sg函数和sg定理
- hdu1851_A Simple Game博弈—SG函数
- 博弈—SG
- sg函数训练——开窍篇
- 博弈——Nim&SG函数函数(hdu1848)
- hdu——3032(博弈之寻找sg函数)
- 博弈——sg函数实现和讲解
- 博弈入门—NIM&SG
- SG函数 博弈
- 博弈之sg函数
- 博弈のSG函数
- hoj1848 Sg函数博弈
- 博弈sg函数
- 博弈SG函数
- 博弈sg函数
- 博弈通解:SG函数
- 博弈 SG函数
- C语言扫雷基础
- Android API Guides---Spinners
- 虚拟机和主机互ping不通
- Java向mysql中插入数据
- 工厂模式的应用--简单计算器
- 博弈训练——sg函数
- 基础知识之深入分析malloc和new
- Java基础(一)——JVM、JRE、JDK、Javac等
- 深入理解JVM(一)——JVM内存模型
- java中double和float区别
- GL version 2.1 with the gpu_shader extension is not supported...
- HashMap与HashTable的区别
- 一个很特别的动态规划入门教程
- 交换机开发(三)—— 深入分析三层网络交换机的原理和设计