Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2844 | Accepted: 1036 |
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)
sg函数
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <set> 6 7 using namespace std; 8 9 const int MAX_N = 205; 10 int dp[MAX_N][MAX_N]; 11 12 int grundy(int w, int h) { 13 if(dp[w][h] != -1) return dp[w][h]; 14 15 set<int> s; 16 for(int i = 2; w - i >= 2; i++) { 17 s.insert(grundy(i, h) ^ grundy(w - i,h)); 18 } 19 for(int i = 2; h - i >= 2; ++i) { 20 s.insert(grundy(w, i) ^ grundy(w, h - i)); 21 } 22 23 int res = 0; 24 while(s.count(res)) res++; 25 return dp[w][h] = res; 26 } 27 28 int main() 29 { 30 //freopen("sw.in","r",stdin); 31 int w, h; 32 33 for(int i = 1; i <= 200; ++i) { 34 for(int j = 1; j <= 200; ++j) dp[i][j] = -1; 35 } 36 while(~scanf("%d%d",&w,&h)) { 37 printf("%s ",grundy(w, h) ? "WIN" : "LOSE"); 38 } 39 //cout << "Hello world!" << endl; 40 return 0; 41 }