HDU S-Nim(博弈 SG)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1536

Problem 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 heaps, all containing some, not necessarily equal, number of beads.

The players take turns chosing a heap and removing a positive number of beads from it.

The first player not able to make a move, loses.

Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:

Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).

If the xor-sum is 0, too bad, you will lose.

Otherwise, move such that the xor-sum becomes 0. This is always possible.

It is quite easy to convince oneself that this works. Consider these facts:

The player that takes the last bead wins.

After the winning player‘s last move the xor-sum will be 0.

The xor-sum will change after every move.

Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.

Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove
a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?

your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position.
This means, as expected, that a position with no legal moves is a losing position.

Input

Input consists of a number of test cases. For each test case: The first line contains a number k (0 < k ≤ 100 describing the size of S, followed by k numbers si (0 < si ≤ 10000) describing S. The second line contains a number m (0
< m ≤ 100) describing the number of positions to evaluate. The next m lines each contain a number l (0 < l ≤ 100) describing the number of heaps and l numbers hi (0 ≤ hi ≤ 10000) describing the number of beads in the heaps. The last test case is followed by
a 0 on a line of its own.

Output

For each position: If the described position is a winning position print a ‘W‘.If the described position is a losing position print an ‘L‘. Print a newline after each test case.

Sample Input

2 2 5
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0

Sample Output

LWW
WWL

题意:

首先输入K 表示一个集合的大小  之后输入集合 ,表示对于这堆石子只能取这个集合中的某个元素的个数;

之后输入一个m 表示接下来对于这个集合要进行m次询问:

之后m行,每行输入一个n 表示有n个堆,每堆有n1个石子  问这一行所表示的状态是赢还是输 如果赢输入W否则L

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[117], sg[10017];
int n;//n是集合s的大小,S[i]是定义的特殊取法规则的数组
int SG_dfs(int x)
{
    if(sg[x] != -1)
        return sg[x];
    int vis[117];
    memset(vis,0,sizeof(vis));
    for(int i = 0; i < n; i++)
    {
        if(x >= s[i])
        {
            SG_dfs(x-s[i]);
            vis[sg[x-s[i]]] = 1;
        }
    }
    int e;
    for(int i = 0; ; i++)
    {
        if(!vis[i])
        {
            e=i;
            break;
        }
    }
    return sg[x] = e;
}
int main()
{
    int m;
    while(scanf("%d",&n)&&n)
    {
        for(int i = 0; i < n; i++)
            scanf("%d",&s[i]);
        memset(sg,-1,sizeof(sg));
        sort(s,s+n);//从小到大
        int num, t;
        scanf("%d",&m);
        for(int i = 0; i < m; i++)
        {
            scanf("%d",&t);
            int ans = 0;
            for(int j = 0; j < t; j++)
            {
                scanf("%d",&num);
                ans^=SG_dfs(num);
            }
            if(ans == 0)
                printf("L");
            else
                printf("W");
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-13 18:40:32

HDU S-Nim(博弈 SG)的相关文章

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

hdu 5011 (nim博弈模版)

//nim博弈 //有n堆石头,两人轮流每次从一堆中拿至少1,之多全部的石头,没有石头可拿为lose //判断先手是win还是lose # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int main() { int n,i; __int64 a,sum; while(~scanf("%d",&n)) { sum=0; fo

hdu 1907(Nim博弈)

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4407    Accepted Submission(s): 2520 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1730 Nim博弈

Tom和Jerry正在玩一种Northcott游戏,可是Tom老是输,因此他怀疑这个游戏是不是有某种必胜策略,郁闷的Tom现在向你求救了,你能帮帮他么? 游戏规则是这样的:  如图所示,游戏在一个n行m列(1 ≤ n ≤ 1000且2 ≤ m ≤ 100)的棋盘上进行,每行有一个黑子(黑方)和一个白子(白方).执黑的一方先行,每次玩家可以移动己方的任何一枚棋子到同一行的任何一个空格上,当然这过程中不许越过该行的敌方棋子.双方轮流移动,直到某一方无法行动为止,移动最后一步的玩家获胜.Tom总是先下

hdu 5795 A Simple Nim 博弈sg函数

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick an

hdu 1536 NIM博弈 (模板)

推荐文章 博弈论初步:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561002.html 博弈解决思想:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561005.html NIM游戏:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561008.html 关于SG函数:http://www.cnblogs.com/Knuth/archive

HDU 3032 (Nim博弈变形) Nim or not Nim?

博弈的题目,打表找规律还是相当有用的一个技巧. 这个游戏在原始的Nim游戏基础上又新加了一个操作,就是游戏者可以将一堆分成两堆. 这个SG函数值是多少并不明显,还是用记忆花搜索的方式打个表,规律就相当显然了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 100; 5 int sg[maxn * 2]; 6 bool vis[maxn * 2]; 7 8 int mex(int v) 9 { 10 if(

HDU 1850 (Nim博弈 取胜方案数) Being a Good Boy in Spring Festival

考虑到Bouton定理的证明过程,设n个数的Nim和(异或和)为X,其最高位的1在第k位,那么n个数中一定有个y的第k为也是个1. 将y的数量变为X xor y,那么n的数的Nim和为0,便转为先手必败局面. 所以先手有多少种取法,就看n个数里面有多少个y,满足二进制的第k为是个1. 1 #include <cstdio> 2 3 const int maxh = 20; 4 const int maxn = 100 + 10; 5 int a[maxn]; 6 7 int main() 8