poj2311 Cutting Game-----sg

2366 단어 바둑eachinputtypes

 
Cutting Game
Time Limit: 1000MS
 
Memory Limit: 65536K
Total Submissions: 2015
 
Accepted: 710
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

Source
POJ Monthly,CHEN Shixi(xreborner)
 
누가 먼저 한 칸씩 자르면 이긴다.
2, 3은 모두 종지 상태이고 3*2, 2*3, 2*2를 모두 종지 상태로 간주하기 때문에 2부터 순환한다
 
#include
#include
#include
#include
using namespace std;
int sg[210][210];
int dfs(int n,int m)
{
    if(sg[n][m]>=0) return sg[n][m];
    bool g[210]={0};
    for(int i=2;i<=n/2;i++)
    {
        g[dfs(i,m)^dfs(n-i,m)]=1;
    }
    for(int i=2;i<=m/2;i++)
    {
        g[dfs(n,i)^dfs(n,m-i)]=1;
    }
    for(int i=0;;i++)
    if(g[i]==0) return sg[n][m]=i;
}
int main()
{
    memset(sg,-1,sizeof(sg));
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(dfs(n,m)>0) puts("WIN");
        else puts("LOSE");
    }
}

좋은 웹페이지 즐겨찾기