讓我們來寫Sg函數處理的博弈吧~


還未摘錄完全

什麼是SG函數

首先,所有的terminal position所對應的頂點,也就是沒有出邊的頂點,其SG值爲0,因爲它的後繼集合是空集。
然後對於一個g(x)=0的頂點x,它的所有前驅y都滿足 g(y)!=0。
對於一個g(x)!=0的頂點,必定存在一個後繼y滿足g(y)=0。

SG函數與“遊戲的和”的概念不是讓我們去組合、製造稀奇古怪的遊戲,
而是把遇到的看上去有些複雜的遊戲試圖分成若干個子游戲,
對於每個比原遊戲簡化很多的子游戲找出它的SG函數,
然後全部異或起來就得到了原遊戲的SG函數,就可以解決原遊戲了。


NITOJ-162 [點我]

題意:
有N個數,現在有兩個要用這M種方法取,當一方不能取時爲輸。
思路:
基礎Sg.

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define mem(a, b) memset(a, b, sizeof(a))

const int Maxn = 1e5 + 10;

int n, m;
int a[Maxn], b[110];
int dp[Maxn];

void DFS(int u){
    int temp;
    if(dp[u] != -1) return;
    int vis[101]={0};
    for(int i=1;i<=m;i++){
        temp = u - b[i];
        if(temp < 0) break;
        DFS(temp);
        vis[dp[temp]] = 1;
    }
    int p=0;
    while(vis[p])
        p++;
    dp[u] = p;
}


int main(){
    int Max;
    while(~scanf("%d%d", &n, &m)){
        Max = 0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d", &a[i]);
            Max = max(a[i], Max);
        }
        sort(a+1, a+n+1);
        for(int i=1;i<=m;i++) scanf("%d", &b[i]);
        sort(b+1, b+1+m);
        memset(dp, -1, sizeof(dp));
        dp[0] = 0;
        for(int i=n;i>=1;i--)
            DFS(a[i]);
        int ans = 0;
        for(int i=1;i<=n;i++){
            ans = ans ^ dp[a[i]];
        }
        if(ans) puts("ZSQ");
        else puts("BJH");
    }
    return 0;
}

Nitoj-223 [點我]

題意:
有一堆數量N的數,按照一個規定去取,然後最終不能取的人失敗。
思路:
基礎SG.

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define mem(a, b) memset(a, b, sizeof(a))

const int Maxn = 1e6 + 10;

int n;
int a[Maxn], num;
int dp[Maxn];

void DFS(int u){
    int temp;
    if(dp[u] != -1) return;
    int vis[40]={0};
    for(int i=1;i<=num;i++){
        temp = u - a[i];
        if(temp < 0) break;
        DFS(temp);
        vis[dp[temp]] = 1;
    }
    int p=0;
    while(vis[p])
        p++;
    dp[u] = p;
}


int main(){
    int Max;
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    for(int i=4;;i++)
    {
        Max = a[i-1] + a[i-3];
        if(Max > 1000000) break;
        a[i] = Max;
        num = i;
    }
    for(int i=1;i<=1000000;i++) dp[i] = -1;
    dp[0] = 0;
    for(int i=1;i<=1000000;i++)
        DFS(i);
    while(~scanf("%d", &n)){
        if(dp[n]) puts("6661");
        else puts("6662");
    }
    return 0;
}
發佈了799 篇原創文章 · 獲贊 125 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章