POJ2311 Cutting Game SG函数

20 篇文章 0 订阅

题目链接:POJ2311

Cutting Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3850 Accepted: 1434

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
题意:一个游戏,每次把一块布剪成2半(整数),剪到1*1就算赢,问给出布的长宽,先手会赢还是输。

题目分析:典型的博弈论题目,首先考虑SG函数,对于一块w*h的布有2种剪法,剪w或者h,设剪成i的长度,那么有i<w-i或h-i,把所有的剪到的sg值异或起来就是sg[w][h]的值。但是考虑到求sg函数时间复杂度太高O(N^3),因此可以先打表。发现异或和为0的只有那么几个数,在sg中把这些点标记出来,其他不用管就好了。

//
//  main.cpp
//  POJ2311
//
//  Created by teddywang on 2016/8/11.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sg[205][205];

int  get_sg(int n,int m)//打表函数,没有用到
{
    if(sg[n][m]!=-1) return sg[n][m];
    int vis[1000];
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=n-i;i++)
    {
        vis[get_sg(i, m)^get_sg(n-i,m)]=1;
    }
    for(int i=2;i<=m-i;i++)
    {
        vis[get_sg(n, i)^get_sg(n,m-i)]=1;
    }
    for(int i=0;;i++)
    {
        if(!vis[i]) return i;
    }
    
}

void init()
{
    int s[100]={2,3,7, 11, 17, 23, 27, 31, 37, 41, 45, 57, 61 ,65, 75, 79, 91, 95, 99, 109, 113, 125, 129, 133, 143, 147, 159, 163, 167, 177, 181, 193, 197 };
    for(int i=0;s[i];i++)
    {
        for(int j=0;s[j];j++)
        {
            sg[s[i]][s[j]]=0;
        }
    }
}

int main()
{
    memset(sg,-1,sizeof(sg));
    init();
    sg[1][1]=0;
   /* for(int i=2;i<=200;i++)
    {
        for(int j=2;j<=200;j++)
        {
            sg[i][j]=get_sg(i,j);
            if(sg[i][j]==0) printf("%d ",j);
        }
        printf("\n");
    }*/
    int w,h;
    while(cin>>w>>h)
    {
        if(sg[w][h]==0) printf("LOSE\n");
        else printf("WIN\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值