【POJ 2311】 Cutting Game

【题目链接】

            http://poj.org/problem?id=2311

【算法】

           博弈论——SG函数

【代码】

           

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
#define MAXN 210

int n,m;
int sg[MAXN][MAXN];

inline int getSG(int n,int m)
{
        int i;
        bool s[1010];
        if (sg[n][m] != -1) return sg[n][m];
        memset(s,false,sizeof(s));
        for (i = 2; i <= n - 2; i++) s[getSG(n-i,m)^getSG(i,m)] = true;
        for (i = 2; i <= m - 2; i++) s[getSG(n,i)^getSG(n,m-i)] = true;
        for (i = 0; ; i++)
        {
                if (!s[i])
                        return sg[n][m] = i;
        }
}

int main() 
{
        
        memset(sg,255,sizeof(sg));
        while (scanf("%d%d",&n,&m) != EOF)
        {
                printf(getSG(n,m)?"WIN\n":"LOSE\n");        
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9298908.html