POJ2311 Cutting Game(博弈Multi-SG函数)

来源:互联网 发布:three.js开发指南 编辑:程序博客网 时间:2024/06/05 14:20

Description

Urej loves to play various types of dull games. He usually asks other people to play with him. He says that playing those games can show his extraordinary wit. Recently Urej takes a great interest in a new game, and Erif Nezorf becomes the victim. To get away from suffering playing such a dull game, Erif Nezorf requests your help. The game uses a rectangular paper that consists of W*H grids. Two players cut the paper into two pieces of rectangular sections in turn. In each turn the player can cut either horizontally or vertically, keeping every grids unbroken. After N turns the paper will be broken into N+1 pieces, and in the later turn the players can choose any piece to cut. If one player cuts out a piece of paper with a single grid, he wins the game. If these two people are both quite clear, you should write a problem to tell whether the one who cut first can win or not.
Input

The input contains multiple test cases. Each test case contains only two integers W and H (2 <= W, H <= 200) in one line, which are the width and height of the original paper.
Output

For each test case, only one line should be printed. If the one who cut first can win the game, print “WIN”, otherwise, print “LOSE”.
Sample Input

2 2
3 2
4 2
Sample Output

LOSE
LOSE
WIN

大致题意:给出n×m的方格纸片,每次只能剪一刀,两个人轮流剪,最先得到1×1纸片的人获胜。

思路:剪一个纸片的时候会把它剪成两个,那么这个纸片的后继的SG取值应该是这两个纸片的SG值取异或。

代码如下

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<map>using namespace std;const int N=200;const int max_n=205;int n,m;int sg[max_n][max_n];bool vis[max_n];void get_sg(){    memset(sg,0,sizeof(sg));    for(int i=2;i<=N;i++)    for(int j=2;j<=N;j++)    {        memset(vis,0,sizeof(vis));        for(int k=2;k<i-1;k++)            vis[sg[k][j]^sg[i-k][j]]=true;        for(int k=2;k<j-1;k++)            vis[sg[i][k]^sg[i][j-k]]=true;        for(int k=0;;k++)        if(!vis[k])        {            sg[i][j]=k;            break;            }       }}int main(){    get_sg();    while(cin>>n>>m)    {        if(!sg[n][m]) cout<<"LOSE\n";        else cout<<"WIN\n";    }    return 0;}
0 0