zoukankan      html  css  js  c++  java
  • HDU1536&&POJ2960 S-Nim(SG函数博弈)

    Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

     Status

    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

    经典的Nim游戏题目中已经给出了——每一堆选取的数量没有限制。

    S-Nim游戏仅仅是限制了每一次从每一堆中选取的个数,依旧用sg函数计算即可。

    经典的Nim游戏中sg(x) = x,所以结果就是每一堆的状态直接xor即可,S-Nim游戏先计算每一堆的sg函数值,然后判断方法依旧是用xor。

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 102
    int n,m,l,s[N],sg[10005],ans,a;
    bool vis[10005];
    void c()
    {
        sg[0]=0;
        for(int i=1;i<=10000;i++)
        {
            memset(vis,0,sizeof(vis));
            for(int j=0;j<n&&s[j]<=i;j++)
            vis[sg[i-s[j]]]=1;
            for(int x=0;x<=10000;x++)
                if(!vis[x])
            {
                sg[i]=x;
                break;
            }
        }
    }
    int main()
    {
        while(cin>>n&&n)
        {
            for(int i=0;i<n;i++)
                cin>>s[i];
            sort(s,s+n);
            c();
            cin>>m;
            while(m--)
            {
                ans=0;
                cin>>l;
                while(l--)
                {
                    cin>>a;
                    ans^=sg[a];
                }
                if(ans)
                    cout<<"W";
                else
                    cout<<"L";
            }
            cout<<endl;
        }
        return 0;
    }

     对sg函数的理解:

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 102
    int n,m,l,s[N],sg[10005],ans,a;
    bool vis[10005];
    void c()
    {
        sg[0]=0;
        for(int i=1;i<=10000;i++)
        {
            memset(vis,0,sizeof(vis));
            for(int j=0;j<n&&s[j]<=i;j++)
            vis[sg[i-s[j]]]=1; //里面的sg[]是sg[i]的后继 vis[sg[]] 指的是求mex
            for(int x=0;x<=10000;x++)
                if(!vis[x])
            {
                sg[i]=x;
                break;
            }
        }
    }
    int main()
    {
        while(cin>>n&&n)
        {
            for(int i=0;i<n;i++)
                cin>>s[i];
            sort(s,s+n);
            c();
            for(int i=0;i<30;i++)
            cout<<"i: "<<i<<"  sg[i]: "<<sg[i]<<endl;
            /*cin>>m;
            while(m--)
            {
                ans=0;
                cin>>l;
                while(l--)
                {
                    cin>>a;
                    ans^=sg[a];
                }
                if(ans)
                    cout<<"W";
                else
                    cout<<"L";
            }
            cout<<endl;*/
        }
        return 0;
    }

    让我们再来考虑一下顶点的SG值的意义。当g(x)=k时,表明对于任意一个0<=i<k,都存在x的一个后继y满足g(y)=i

    也就是说,当某枚棋子的SG值是k时,我们可以把它变成0、变成1……、变成k-1,但绝对不能保持k不变。

    不知道你能不能根据这个联想到Nim游戏,Nim 游戏的规则就是:每次选择一堆数量为k的石子,

    可以把它变成0、变成1……、变成k-1,但绝对不能保持k不变。这表明,如果将n枚棋子所在的顶点的 SG值看作n

    堆相应数量的石子,那么这个Nim游戏的每个必胜策略都对应于原来这n枚棋子的必胜策略!

  • 相关阅读:
    java读取文件并获得文件编码,转换为指定编码的工具类代码
    OpenStreetMap地图数据介绍(转)
    字符串匹配的KMP算法(转)
    Dijkstra算法求最短路径(java)(转)
    用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)
    用java解析在OpenStreetMap上下载的地图数据
    加载依赖的jar包在命令行编译和运行java文件
    CentOS 加载/挂载 U盘 (转)
    MongoDB:如何正常关闭服务(转)
    JTS(Geometry)(转)
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5620364.html
Copyright © 2011-2022 走看看