sg函数的应用 poj--Cutting Game

来源:互联网 发布:零基础网络美术培训班 编辑:程序博客网 时间:2024/06/05 00:55
Cutting Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4036 Accepted: 1501

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 23 24 2

Sample Output

LOSELOSE

WIN

题目大意:

剪纸游戏,减一张长方形方格纸,每次只能横减或竖减,谁先减除一个单个的方格就赢

思路:首先一定是考虑如何打一下sg函数的表,关键是找出的它的所有后继的情况,当一个x*y的方格纸的后继可以分为两种情况

(1)横减,所有的后继情况为x从2~x-2(一定不能减1,或者x-1,那样就输了)

(2)竖减同理

打完表就解决了,当sg为0时后手赢,否则先手赢

ac代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int sg[205][205];int get_sg(int x,int y){    if(sg[x][y]!=-1)    return sg[x][y];    if(x==1&&y==1)    return sg[x][y]=0;    int vis[205];    memset(vis,0,sizeof(vis));    for(int i=2;i<x-1;i++)    {        int t=get_sg(x-i,y)^get_sg(i,y);        vis[t]=1;    }    for(int i=2;i<y-1;i++)    {       int t=get_sg(x,y-i)^get_sg(x,i);       vis[t]=1;    }    for(int i=0;;i++)    if(vis[i]==0)    return sg[x][y]=i;}int main(){    int x,y;    memset(sg,-1,sizeof(sg));    while(~scanf("%d%d",&x,&y))    {       if(get_sg(x,y)==0)       printf("LOSE\n");       else       printf("WIN\n");    }    return 0;}

0 0