Cutting Game POJ - 2311(SG函数)

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

题意:
W*H的布,你可以横着切纵着切。先切出1X1的人赢。问先手是否必胜

思路:
SG函数的应用。
定义SG[i][j]为长度为i,宽度为j的SG值。
子状态可以是SG[i-a][j]和SG[i][j-b],并且i-a,a,j-b,b都大于1,因为要是你直接切出了1,那后手不就赢了。
然后求所有出边(子状态)异或,最后SG函数值为mex值(最小的未出现过的集和值)

公平组合游戏的介绍:
https://www.cnblogs.com/winlere/p/10849134.html

#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 205;
int SG[maxn][maxn];
int flag[maxn];

void init()
{
    for(int i = 2;i <= 200;i++)
    {
        for(int j = 2;j <= 200;j++)
        {
            memset(flag,0,sizeof(flag));
            for(int a = 2;i - a >= 2;a++)flag[SG[a][j] ^ SG[i - a][j]] = 1;//横着切
            for(int b = 2;j - b >= 2;b++)flag[SG[i][b] ^ SG[i][j - b]] = 1;//纵着切
            for(int k = 0;k <= 200;k++)//mex运算
            {
                if(!flag[k])
                {
                    SG[i][j] = k;
                    break;
                }
            }
        }
    }
}

int main()
{
    init();
    int w,h;
    while(~scanf("%d%d",&w,&h))
    {
        if(SG[w][h])printf("WIN\n");
        else printf("LOSE\n");
    }
    return 0;
}

发布了628 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104012770