POJ 2311 —— 博弈Grundy值

Cutting Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2597   Accepted: 948

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的人获胜,问先手是胜是负。

思路:当一张纸被剪后,他的Grundy值 = g1 ^ g2。所以只要Grundy值相同,即使剪开,我们对剪成的各部分的Grundy值异或,本身的Grundy值不变。所以我们只要枚举所有一步能转移到的状态的Grundy值,就能计算整体的Grundy值了。

注意的一点是,只要出现长或宽为1的情况,下一步一定会出现1*1的格子,显然是必败态,这些状态的Grundy值为0,不必再处理了,所以我们枚举时保证长宽至少为2。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 200 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int w , h;
int vis[MAXN][MAXN];
int grundy(int w  , int h)
{
    if(vis[w][h] != -1)return vis[w][h];
    set <int> S;
    for(int i = 2 ; i <= w - 2 ; i++)S.insert(grundy(i,  h) ^ grundy(w - i , h));
    for(int i = 2 ; i <= h - 2 ; i++)S.insert(grundy(w , i) ^ grundy(w , h - i));
    int ans = 0;
    while(S.count(ans))ans++;
    return vis[w][h] = ans;
}
int main()
{
    clr(vis , -1);
    while(~scanf("%d%d" , &w , &h))
    {
        if(grundy(w , h) != 0)puts("WIN");
        else puts("LOSE");
    }
    return 0;
}


你可能感兴趣的:(ACM,博弈,Grundy值)