SG Game function template

Source: Internet
Author: User

The following two templates should be more rigorous, the inside of the f[] is zero-based.

Reprint Source: Transfer from: http://blog.csdn.net/primoblog/article/details/13376057

1. SG Play table

1 //f[]: The number of stones that can be taken away2 //sg[]:0~n value of SG function3 //hash[]:mex{}4 intF[k],sg[n],hash[n]; 5 voidGETSG (intN)6 {  7memset (SG,0,sizeof(SG)); 8          for(intI=1; i<=n; i++) {  9memset (Hash,0,sizeof(hash)); Ten                  for(intj=0; F[j]<=i && J < K; J + +)//k is the effective length of f[] Onehash[sg[i-f[j]]]=1;  A                  for(intj=0; ; J + +) {//The smallest non-negative integer that is not present in mes{} -                         if(hash[j]==0) {   -sg[i]=J;  the                                  Break;  -                         }   -                 }   -         }   +}

2dfs

    //Note that the S array to sort by from small to large the SG function is initialized to-1 for each collection simply initialize 1 times//N is the size of the set S S[i] is an array of special rules that are defined    intS[n],sg[n],n; intGETSG (intx) {if(sg[x]!=-1)                        returnSg[x]; BOOLVis[m]; memset (Vis,0,sizeof(VIS));  for(intI=0; i<n; i++) {                        if(x>=S[i]) VIS[GETSG (x-s[i])]=1; }                 for(i=0;; i++)                        if(!Vis[i]) {Sg[x]=i;  Break; }                returnSg[x]; }   

SG Game function template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.