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 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
自己SB了。。竟然TLE了好几次。。
求SG函数值。sg[w][h]=met{sg[i][h]^sg[w-i][h],sg[w][i]^sg[w][h-i]|i>=2};包含所有w,h的后继结点的sg值。然后求met。
代码:
#include <stdio.h>
#include <string.h>
#define MAX 250
int w , h ;
int sg[MAX][MAX] ;
int getSg(int x , int y)
{
	if(sg[x][y] != -1)
		return sg[x][y] ;
	bool visited[MAX*5] ;
	memset(visited,false,sizeof(visited)) ;
	for(int i = 2 ; i <= x-i ; ++i)
	{
		visited[getSg(i,y)^getSg(x-i,y)] = true ;
	}
	for(int j = 2 ; j <= y-j ; ++j)
	{
		visited[getSg(x,j)^getSg(x,y-j)] = true ;
	}
	for(int i = 0 ; ; ++i)
	{
		if(!visited[i])
		{
			return sg[x][y] = i ;
		}
	}
}

int main()
{
	memset(sg,-1,sizeof(sg)) ;
	sg[2][2] = sg[3][2] = sg[2][3] = 0 ;
	while(scanf("%d%d",&w,&h) != EOF)
	{

		if(getSg(w , h))
			puts("WIN") ;
		else
			puts("LOSE") ;
	}
	return 0 ;
}

			与君共勉
时间: 2024-10-21 18:16:28

poj 2311 Cutting Game SG函数的运用 唉,,,的相关文章

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

hdu 1536 S-Nim|| poj 2960 S-Nim (sg函数)

#include <stdio.h> #include <string.h> int s[110]; int sg[10010],hash[110]; int n, m; int getsg(int x) //sg模板 { int i; if(sg[x] != -1) return sg[x]; memset(hash,0,sizeof(hash)); for(i = 0; i < n; i++) { if(x >= s[i]) { sg[x - s[i]] = get

POJ 2960 S-Nim 博弈论 sg函数

http://poj.org/problem?id=2960 sg函数几乎是模板题. 调试代码的最大障碍仍然是手残在循环里打错变量名,是时候换个hydra产的机械臂了[超想要.jpg] 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<iostream> 6 #include<map> 7 using na

poj 2960 S-Nim(SG函数)

S-Nim Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3694   Accepted: 1936 Description Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows: The starting position has a number of h

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)

题意: 有一张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 2960 S-Nim(SG函数模板题)

链接:https://vjudge.net/problem/POJ-2960 题意:每行输入首先给出一个数k,代表集合S的大小,接下来紧跟着k个数,表示集合S里的数.接下来一行数为m代表有m个游戏,后面m行每行第一个数字为n代表有n堆石子,后面紧跟着n个数代表每堆石子的个数.多组数据,做到0结束 对于每组数据,我们要输出n个字母,第i个字母为“W”代表第i个游戏先手必胜,“L”代表第i个游戏先手必败,做完一组数据后换行. 题解:模板题 #include <cstdio> #include &l

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

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 th