POJ-2311 Cutting Game(SG函数,二维)

Cutting Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2750   Accepted: 1009

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函数,二维的sg数组,初学有点不好想;

      sg[x][y] = met{ sg[i][y] ^ sg[x-i][y], sg[x][i] ^ sg[x][y-i] | 2 <= i  };

      例如:3*3的矩形有2种剪法,sg[3][3] = met(sg[1][3], sg[2][3], sg[3][1], sg[3][2]);

      这里是不会剪出1*n或者n*1这样的矩形,因为这样对方必赢。所以必败态是2*2 、2*3或者3*2。

代码:

#include <stdio.h>
#include <string.h>
#define N 205

int sg[N][N];

int getSG(int x, int y)
{
	if(sg[x][y] != -1)
		return sg[x][y];

	int i, vis[1005];
	memset(vis, 0, sizeof(vis));
	for(i = 2; i <= x - i; i ++)
		vis[ getSG(i, y) ^ getSG(x - i, y) ] = 1;
	for(i = 2; i <= y - i; i ++)
		vis[ getSG(x, i) ^ getSG(x, y - i) ] = 1;

	for(i = 0; ; i ++){
		if(vis[i] == 0)
			return sg[x][y] = i;
	}
}

int main()
{
	int m, n;
	memset(sg, -1, sizeof(sg));

	while(scanf("%d%d", &m, &n) != EOF){
		if(getSG(m, n))
			printf("WIN\n");
		else
			printf("LOSE\n");
	}

	return 0;
}


你可能感兴趣的:(博弈,sg函数)