博弈,求sg值

来源:互联网 发布:电子与通信工程知乎 编辑:程序博客网 时间:2024/05/22 14:56
  1. #include <iostream>  
  2. using namespace std;  
  3. #define MAX 1005  
  4. /* 
  5. 计算从1-n范围内的SG值。 
  6. Array(存储可以走的步数,Array[0]表示可以有多少种走法) 
  7. Array[]需要从小到大排序 
  8. /*HDU1847博弈SG函数 
  9. 1.可选步数为1-m的连续整数,直接取模即可,SG(x) = x % (m+1); 
  10. 2.可选步数为任意步,SG(x) = x; 
  11. 3.可选步数为一系列不连续的数,用GetSG(计算) 
  12. */  
  13. int SG[MAX], hash[MAX];  
  14. void GetSG(int Array[], int n = MAX-1)  
  15. {  
  16.     int i, j;  
  17.     memset(SG, 0, sizeof(SG));  
  18.     for(i = 0; i <= n; i++)  
  19.     {  
  20.         memset(hash, 0, sizeof(hash));  
  21.         for(j = 1; j <= Array[0]; j++)  
  22.         {  
  23.             if(i < Array[j])  
  24.                 break;  
  25.             hash[SG[i - Array[j]]] = 1;  
  26.         }  
  27.         for(j = 0; j <= n; j++)  
  28.         {  
  29.             if(hash[j] == 0)  
  30.             {  
  31.                 SG[i] = j;  
  32.                 break;  
  33.             }  
  34.         }  
  35.     }  
  36. }  
原创粉丝点击