Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4813 | Accepted: 1764 |
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
题意:给一张纸a*b,两人轮流水平或竖直剪,谁先剪到单位正方形谁就赢,求先手的输赢情况。
思路:谁先简称1*x的情况谁就输,所以转换成竖直方向和水平方向两个子问题,再利用SG函数求解。(游戏的和的SG函数值是它的所有子游戏的SG函数值的异或)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
//公平组合博弈SG函数模板 SG[x]相当于Nim博弈中一堆石子的个数
//SG[]:0~n的SG函数值
//S[]:为x后继状态的集合
const int MAXN=205;
int SG[MAXN][MAXN],S[MAXN];
int getSG(int a,int b)
{
if(SG[a][b]!=-1)return SG[a][b];
memset(S,0,sizeof(S));
for(int i=2;a-i>=i;i++)
{
S[getSG(i,b)^getSG(a-i,b)]=1;
}
for(int i=2;b-i>=i;i++)
{
S[getSG(a,i)^getSG(a,b-i)]=1;
}
for(int i=0;;i++)
if(!S[i])return SG[a][b]=i;
}
int main()
{
int a,b;
memset(SG,-1,sizeof(SG));
while(~scanf("%d%d",&a,&b))
{
if(getSG(a,b))printf("WIN\n");
else printf("LOSE\n");
}
}