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>
# include<iostream>
# include<string.h>
using namespace std;
# define N 210
int vis[N],sg[N][N];
int GetSG(int n,int m)
{
    int i;
    if(sg[n][m]!=-1)
        return sg[n][m];
    memset(vis,0,sizeof(vis));
    for(i=2; i<=n-i; i++)
        vis[GetSG(i,m)^GetSG(n-i,m)]=1;
    for(i=2; i<=m-i; i++)
        vis[GetSG(n,i)^GetSG(n,m-i)]=1;
    for(i=0;; i++)
        if(vis[i]==0)
            return sg[n][m]=i;
}
int main()
{
    int n,m;
    memset(sg,-1,sizeof(sg));
    while(~scanf("%d%d",&n,&m))
    {
        if(GetSG(n,m))
            printf("WIN\n");
        else
            printf("LOSE\n");
    }
    return 0;
}
时间: 2024-11-07 19:51:10

POJ 2311 Cutting Game (sg函数)的相关文章

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

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