poj2311 Cutting Game

9 篇文章 0 订阅

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.

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.

POJ Monthly,CHEN Shixi(xreborner)

来自 http://poj.org/problem?id=2311

Solution


许久不上poj
仔细阅读发现1个格子是赢的条件,也就是玩家不会选择剪一条1*x的格子,那么对于2*2、2*3、3*2、3*3的格子都是必败态
上sg函数,注意到可以预处理然后O(1)出解,这样就非常快了

Code


#include <stdio.h>
#include <string.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))

const int N=205;

int sg[N][N],rec[N],id;

void get_sg(int n,int m) {
    if (~sg[n][m]) return ;
    id++;
    rep(i,2,n-2) rec[sg[i][m]^sg[n-i][m]]=id;
    rep(i,2,m-2) rec[sg[n][i]^sg[n][m-i]]=id;
    for (int i=0;;i++) if (rec[i]!=id) {
        sg[n][m]=i;
        return ;
    }
}

int main(void) {
    fill(sg,-1);
    sg[2][3]=sg[3][2]=0;
    sg[2][2]=0;
    rep(i,2,N-1) rep(j,2,N-1) get_sg(i,j);
    int n,m;
    while (~scanf("%d%d",&n,&m)) {
        if (sg[n][m]) puts("WIN");
        else puts("LOSE");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值