Cutting Game
Time Limit: 1000MS
Memory Limit: 65536K
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
题意
一个n*m的网格,每次可以水平切割或垂直切割,第一个切出1*1大小的获胜,问先手胜负
题解
若当前状态无论怎么切割都会获得一个1*n(n>1)的网格,则为必败态,每次切割后会产生两个子游戏,利用sg函数求解,定义sg[1][1]=1(初始网格最小2*2,1*1的网格一定是上一个玩家剪下的)。AC代码如下
#include <cstdio>
#include <cstring>
using namespace std;
int W,H;
int sg[205][205];
int ans(int w,int h)
{
if(sg[w][h]!=-1)
return sg[w][h];
bool vis[205];
memset(vis,0,sizeof vis);
for(int i=2;i<=h/2;i++)
vis[ans(w,i)^ans(w,h-i)]=1;
for(int i=2;i<=w/2;i++)
vis[ans(i,h)^ans(w-i,h)]=1;
for(int i=0;i<200;i++)
if(vis[i]==0)
return sg[w][h]=sg[h][w]=i;
}
int main()
{
memset(sg,-1,sizeof sg);
sg[1][1]=0;
while(scanf("%d%d",&W,&H)!=EOF)
{
if(ans(W,H))
printf("WIN\n");
else
printf("LOSE\n");
}
return 0;
}