码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2311 Cutting Game SG函数的运用 唉,,,

时间:2015-04-02 09:08:58      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:poj2311   cutting game   博弈   sg函数   

Cutting Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3227   Accepted: 1195

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
自己SB了。。竟然TLE了好几次。。
求SG函数值。
sg[w][h]=met{sg[i][h]^sg[w-i][h],sg[w][i]^sg[w][h-i]|i>=2};
包含所有w,h的后继结点的sg值。然后求met。
代码:
#include <stdio.h>
#include <string.h>
#define MAX 250
int w , h ;
int sg[MAX][MAX] ;
int getSg(int x , int y)
{
	if(sg[x][y] != -1)
		return sg[x][y] ;
	bool visited[MAX*5] ;
	memset(visited,false,sizeof(visited)) ;
	for(int i = 2 ; i <= x-i ; ++i)
	{
		visited[getSg(i,y)^getSg(x-i,y)] = true ;
	}	
	for(int j = 2 ; j <= y-j ; ++j)
	{
		visited[getSg(x,j)^getSg(x,y-j)] = true ;
	}
	for(int i = 0 ; ; ++i)
	{
		if(!visited[i])
		{
			return sg[x][y] = i ;
		}
	}
}

int main()
{
	memset(sg,-1,sizeof(sg)) ;
	sg[2][2] = sg[3][2] = sg[2][3] = 0 ;
	while(scanf("%d%d",&w,&h) != EOF)
	{
		
		if(getSg(w , h))
			puts("WIN") ;
		else
			puts("LOSE") ;
	}
	return 0 ;
}


			与君共勉

poj 2311 Cutting Game SG函数的运用 唉,,,

标签:poj2311   cutting game   博弈   sg函数   

原文地址:http://blog.csdn.net/lionel_d/article/details/44827489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!