当前位置: 代码迷 >> 综合 >> POJ 2311 Cutting Game【博弈论---SG函数】
  详细解决方案

POJ 2311 Cutting Game【博弈论---SG函数】

热度:112   发布时间:2023-11-28 07:00:58.0

题目链接:http://poj.org/problem?id=2311

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

题解:P181。   若两人都采取最优策略行动,则他们一定不会剪出 1 * X 或 X * 1 的纸张。否则对手就可以剪出 1*1 从而立即获胜。除了1*X 和 X*1 的纸张外,能够剪出 1*1 的方法必定要经过 2*2,2*3 和 3*2 三种局面之一。这三种局面为必败局面。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 202;
int sg[maxn][maxn];
int v[maxn*5];
int SG(int n, int m) {if(sg[n][m] != -1){return sg[n][m];}memset(v, 0, sizeof v);for(int i = 2; i <= n-i; i++)v[SG(i,m)^SG(n-i, m)] = 1;for(int i = 2; i <= m-i; i++)v[SG(n, i)^SG(n, m-i)] = 1;for(int i = 0; ; i++)if(v[i] == 0)return sg[n][m]=i;
}int main()
{int n, m;memset(sg, -1, sizeof sg);while(cin >> n >> m) {if(SG(n, m))printf("WIN\n");else printf("LOSE\n");}return 0;
}

 

  相关解决方案