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.
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.
POJ Monthly,CHEN Shixi(xreborner)
来自 http://poj.org/problem?id=2311
Solution
许久不上poj
仔细阅读发现1个格子是赢的条件,也就是玩家不会选择剪一条1*x的格子,那么对于2*2、2*3、3*2、3*3的格子都是必败态
上sg函数,注意到可以预处理然后O(1)出解,这样就非常快了
Code
#include <stdio.h>
#include <string.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
const int N=205;
int sg[N][N],rec[N],id;
void get_sg(int n,int m) {
if (~sg[n][m]) return ;
id++;
rep(i,2,n-2) rec[sg[i][m]^sg[n-i][m]]=id;
rep(i,2,m-2) rec[sg[n][i]^sg[n][m-i]]=id;
for (int i=0;;i++) if (rec[i]!=id) {
sg[n][m]=i;
return ;
}
}
int main(void) {
fill(sg,-1);
sg[2][3]=sg[3][2]=0;
sg[2][2]=0;
rep(i,2,N-1) rep(j,2,N-1) get_sg(i,j);
int n,m;
while (~scanf("%d%d",&n,&m)) {
if (sg[n][m]) puts("WIN");
else puts("LOSE");
}
return 0;
}