POJ 2311 Cutting Game(SG函数)
Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4806 | Accepted: 1760 |
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
分析
dfs求sg值。居然可以这样玩(⊙_⊙)
code
1 #include<cstdio> 2 #include<set> 3 #include<cstring> 4 5 using namespace std; 6 int sg[500][500]; 7 8 9 int get_SG(int w,int h) { 10 if (sg[w][h] != -1) return sg[w][h]; 11 set<int>s; 12 for (int i=2; w-i>=2; ++i) //竖直切 13 s.insert(get_SG(i,h)^get_SG(w-i,h)); 14 for (int i=2; h-i>=2; ++i) //水平切 15 s.insert(get_SG(w,i)^get_SG(w,h-i)); 16 for (int j=0; ; ++j) 17 if (!s.count(j)) {sg[w][h] = j;break;} 18 return sg[w][h]; 19 20 } 21 int main () { 22 memset(sg,-1,sizeof(sg)); 23 int w,h; 24 while (~scanf("%d%d",&w,&h)) { 25 if (get_SG(w,h)==0) puts("LOSE"); 26 else puts("WIN"); 27 } 28 return 0; 29 }