POJ - 2311 Cutting Game

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
#include <cstdio>
#include <set>
#include <cstring>
using namespace std;

int g[210][210];

int grundy(int W, int H) {
	if (g[W][H] != -1) return g[W][H];

	set<int> s;
	for (int i = 2; i <= W / 2; i++) s.insert(grundy(i, H) ^ grundy(W - i, H));
	for (int i = 2; i <= H / 2; i++) s.insert(grundy(W, i) ^ grundy(W, H - i));

	int res = 0;
	while (s.count(res)) res++;
	return g[W][H] = res;
}

int main() {
	int W, H;
	memset(g, -1, sizeof(g));
	while (scanf("%d%d", &W, &H) != EOF) {
		if (grundy(W, H)) puts("WIN");
		else puts("LOSE");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 14:41:33

POJ - 2311 Cutting Game的相关文章

poj 2311 Cutting Game nim与状态的grundy值

题意: 给一个w*h的矩形,两人轮流只能沿格子的边缘横剪或竖剪,最先剪出1*1的格子的人获胜,问先手必胜还是必败. 分析: 此题要求对grundy值有理解.一个全局状态的grundy值是对游戏中某个状态的有效的描述,grundy值描述了当前状态的所有后继状态,比如n堆石子的nim游戏的grundy值是a1^a2^...an. 代码: //poj 2311 //sep9 #include <iostream> #include <set> using namespace std; c

POJ 2311 Cutting Game (sg函数)

给出一个N*M的纸片,每一次可以把一部分剪成两部分,谁剪出1*1的就赢了. http://poj.org/problem?id=2311 对于任何一个人,都不会先剪出1*n或者n*1,应该这样就必败了. 那我们考虑一个状态的后继中,最小的边也是2,这样就可以避免之前的问题,也不需要考虑类似ANTI-SG. 一旦出现2*2,2*3,3*2,这些都成了终止状态,不论怎么剪都会出现1*n,或者n*1 还是考察SG函数 # include<stdio.h> # include<algorithm

poj 2311 Cutting Game SG函数的运用 唉,,,

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

POJ 2311 Cutting Game [Multi-SG?]

传送门 题意:n*m的纸片,一次切成两份,谁先切出1*1谁胜 Multi-SG? 不太一样啊 本题的要求是后继游戏中任意游戏获胜就可以了.... 这时候,如果游戏者发现某一单一游戏他必败他就不会再玩了 $2*2,2*3,3*3$都不会再玩了(除非只剩下这样的纸片了),所以都可以认为是终止状态,必败 在此基础上按照Multi-SG递推就对了 #include <iostream> #include <cstdio> #include <algorithm> #includ

poj 2311 Cutting Game (SG)

题意: 有一张W*H的纸片. 每人每次可以横着撕或者竖着撕,先撕出1*1那一方胜. 数据范围: W and H (2 <= W, H <= 200) 思路: 很好抽象出游戏图的模型,用SG解决.直接看代码. 代码: int dp[maxn][maxn]; int sg(int w,int h){ if(dp[w][h]!=-1) return dp[w][h]; bool g[maxn]; mem(g,false); for(int i=2;i<=w/2;++i) g[sg(i,h)^s

poj 2311

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

poj 3103 Cutting a Block 模拟水题

水题 #include <iostream> using namespace std; int main() { int x,y,z,n; scanf("%d%d%d%d",&x,&y,&z,&n); for(int i=0;i<n;++i) printf("0 0 %.8lf %d %d %.8lf\n",(z*1.0/n)*i,x,y,(z*1.0/n)*(i+1)); return 0; } 版权声明:本文为博

POJ 2378-Tree Cutting(树形dp)

题意: n个节点的树,删除一个点,得到的最大联通分支大小不大于总节点数的一半,求这样点的集合 分析:和上题一样 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <strin

博弈论题目总结(二)——SG组合游戏及变形

SG函数 为了更一般化博弈问题,我们引入SG函数 SG函数有如下性质: 1.如果某个状态SG函数值为0,则它后继的每个状态SG函数值都不为0 2.如果某个状态SG函数值不为0,则它至少存在一个后继的状态SG函数值为0 如果某个局面SG函数值为0,则该局面先手必败 放到有向图中,该有向图的核就是SG值为0的点构成的集合 游戏的和 游戏的和的SG函数值=所有子游戏SG函数值的异或和Xor 如果所有子游戏都进行完毕,那么Xor=0,必败 如果某个状态的SG函数值为0,那么后手一定可以做出一种动作,保持