POJ

来源:互联网 发布:淘宝联盟2016旧版 编辑:程序博客网 时间:2024/06/15 09:50

题目:给你一个w*h的矩形,每次可以选择横向或纵向切割矩形,将其分成2个矩形,N次操作后,就会有N+1个矩形,谁先切割出1*1谁就获胜。

思路:谁先切割出1*1谁就胜,那么就是说谁切割出1*x或x*1谁就输了。然后求SG函数就可以了。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f//0x3f3f3f3fconst int maxn=210;int sg[maxn][maxn];int get_sg(int w,int h){    if(w>h) swap(w,h);    if(sg[w][h]!=-1)        return sg[w][h];    bool mex[1000];    mm(mex,false);    for(int i=2;i<=w;i++){        if(w-i>=2){            int pos=(get_sg(i,h)^get_sg(w-i,h));            mex[pos]=true;        }    }    for(int i=2;i<=h;i++){        if(h-i>=2){            int pos=(get_sg(w,i)^get_sg(w,h-i));            mex[pos]=true;        }    }    for(int i=0;;i++){        if(!mex[i]){            sg[w][h]=i;            break;        }    }    return sg[w][h];}int main(){    int w,h;    mm(sg,-1);    while(~scanf("%d%d",&w,&h)){        int ans=get_sg(w,h);        if(ans>0) printf("WIN\n");        else printf("LOSE\n");    }    return 0;}


原创粉丝点击