Cutting Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4452 | Accepted: 1632 |
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
博弈,递归计算sg函数。
切成1*n的方块后,肯定是必败态,所以考虑状态时边长从2开始。
现在的sg值为子状态sg值的异或,利用这一点推出sg函数。
#include <iostream>
#include <string.h>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
int dp[205][205];
int dfs(int n,int m) {
if (dp[n][m]!=-1) return dp[n][m];
int visit[1024];
memset(visit,0,sizeof(visit));
int i;
for (i=2;i<=n-i;i++) {
dp[i][m]=dfs(i,m);
dp[n-i][m]=dfs(n-i,m);
visit[dp[i][m]^dp[n-i][m]]=1;
}
for (i=2;i<=m-i;i++) {
dp[n][i]=dfs(n,i);
dp[n][m-i]=dfs(n,m-i);
visit[dp[n][i]^dp[n][m-i]]=1;
}
for (i=0;;i++)
if(!visit[i]) return dp[n][m]=i;
}
int main() {
int i,n,m;// 0 lose 1 win
memset(dp,-1,sizeof(dp));
while (scanf("%d%d",&n,&m)!=EOF) {
if (dfs(n,m)) printf("WIN\n"); else printf("LOSE\n");
}
return 0;
}