Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4794 | Accepted: 1753 |
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)
题目链接:http://poj.org/problem?id=2311
【题意】有w*h的格子,每次可以沿着格子线水平或者垂直把一个格子分成两个,如果某人剪出1*1的格子则胜利。
【思路】2*2则显而易见为必败态,则可以通过该状态利用sg函数递推出其他状态。
实现代码1:
#include <cstdio>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
int sg[310][310],w,h;
int getSG(int w, int h){
if(sg[w][h] != -1) return sg[w][h];
set<int>st;
for(int i = 2; w - i >= 2; i ++) st.insert(sg[i][h] ^ sg[w-i][h]);
for(int i = 2; h - i >= 2; i ++) st.insert(sg[w][i] ^ sg[w][h-i]);
int res = 0;
while(st.count(res)) res ++;
return sg[w][h] = res;
}
int main(){
memset(sg,-1,sizeof(sg));
while(~scanf("%d%d",&w,&h)){
if(getSG(w,h)) printf("WIN\n");
else printf("LOSE\n");
}
return 0;
}
#include <cstdio>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
int sg[310][310],w,h;
void getSG(){
memset(sg,0,sizeof(sg));
for(int i = 2; i <= 210; i ++){
for(int j = 2; j <= 210; j ++){
set<int>st;
for(int k = 2; j - k >= 2; k ++) st.insert(sg[i][j-k]^sg[i][k]);
for(int k = 2; i - k >= 2; k ++) st.insert(sg[i-k][j]^sg[k][j]);
int res = 0;
while(st.count(res)) res ++;
sg[i][j] = res;
}
}
}
int main(){
getSG();
while(~scanf("%d%d",&w,&h)){
if(sg[w][h]) printf("WIN\n");
else printf("LOSE\n");
}
return 0;
}