Cutting Game
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 ,每次切w×h的方块,先得到1×1方块的人获胜,问先手获胜还是后手获胜 多组数据 (2 ≤ w, h ≤ 200)
题目链接:http://poj.org/problem?id=2311
对于2 * 2, 3 * 3, 2 * 3,先手必输
对于1 * x (x ≥1) 先手必胜
所以每次切的时候都尽量不得到1 * x形式的格子
然后直接套上去就OK了
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<vector> 7 8 using namespace std; 9 10 const int MAXN = 233; 11 int n, m; 12 int sg[MAXN][MAXN]; 13 bool tf[MAXN * 399]; 14 15 void get_sg() { 16 for (int i = 1; i < MAXN; ++i) sg[1][i] = 1, sg[i][1] = 1; 17 for (int i = 2; i < MAXN; ++i) { 18 for (int j = i; j < MAXN; ++j) { 19 for (int k = 0; k < 2000; ++k) tf[k] = 1; 20 for (int k = 2; k <= i - k; ++k) tf[sg[k][j] ^ sg[i - k][j]] = 0; 21 for (int k = 2; k <= j - k; ++k) tf[sg[i][k] ^ sg[i][j - k]] = 0; 22 int k = 0; 23 while (!tf[k]) ++k; 24 sg[j][i] = sg[i][j] = k; 25 } 26 } 27 } 28 29 int main() { 30 get_sg(); 31 while (scanf("%d %d", &n, &m) != EOF) { 32 if (sg[n][m]) printf("WIN\n"); else printf("LOSE\n"); 33 } 34 return 0; 35 }