Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5342 | Accepted: 1958 |
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)
#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>
#define MAXN 200
using namespace std;
int men[MAXN+1][MAXN+1];
int grundy(int w,int h)
{
int i;
int res=0;
if(men[w][h]!=-1) //因为是一半一半切的,十分像数字三角形,所以要记忆化搜索
return men[w][h]; //记忆化搜索
set <int> s;
for(i=2;w-i>=2;i++) //递归和算阶乘。
s.insert(grundy(i,h)^grundy(w-i,h)); //每一种切割状态都看成是一堆(包括最初的状态)
for(i=2;h-i>=2;i++) //为什么>=2,因为长度为1是,下一个状态就失败,所以当前状态是必败态
//再次强调必败必胜是相对先手来说的。
s.insert(grundy(w,i)^grundy(w,h-i)); //必败态的Grundy的值为0.当作定理吧。
while(s.count(res)) //
res++;
return men[w][h]=res;
}
int main()
{
int w,h;
memset(men,-1,sizeof(men)); //md,这个也很耗时间,所以最好还是只用一次。
while(scanf("%d%d",&w,&h)!=EOF)
{
if(grundy(w,h)!=0)
printf("WIN\n");
else
printf("LOSE\n");
}
return 0;
}