POJ 2311 (博弈 sg函数)

Cutting Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3544   Accepted: 1318

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

Source

POJ Monthly,CHEN Shixi(xreborner)

题意:给出一张w*h的纸,两个人轮流选择一个小纸片裁成两片,先裁出1*1纸片的人获胜,问先手是不是必胜。

每张纸片都有若干个后继状态,每个后继状态有两个纸片,这两个纸片相当于两个子游戏可以用两个纸片对应的sg函数的亦或和表示这个子状态的sg函数。显然有一条边小于2就是必败态。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 211

int s[maxn][maxn];
int n, m;
bool vis[4111];

int sg (int a, int b) {
    memset (vis, 0, sizeof vis);
    for (int i = 2; a-i >= 2; i++)
        vis[s[i][b]^s[a-i][b]] = 1;
    for (int i = 2; b-i >= 2; i++)
        vis[s[a][i]^s[a][b-i]] = 1;
    for (int i = 0; ; i++)
        if (!vis[i])
            return i;
    return 1;
}

void init () {
    for (int i = 2; i <= 200; i++) {
        for (int j = 2; j <= 200; j++) {
            s[i][j] = sg (i, j);
        }
    }
}

int main () {
    init ();
    while (cin >> n >> m) {
        cout << (s[n][m] ? "WIN" : "LOSE") << endl;
    }
    return 0;
}


你可能感兴趣的:(POJ 2311 (博弈 sg函数))