• hdu1536 S-Nim


    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5856    Accepted Submission(s): 2507


    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
     

    Source
     
    题意:给你一个集合S,再给你几个局面,每次操作某一堆只能减去S中的一个数,问当前局面是必胜局面还是必败局面。
    思路:这题是sg模板题,因为终止条件是最后不能走的人输,所以当各个游戏的sg值异或和为0时为必败局面,不为0为必胜局面,我们只要处理每个游戏的sg值就行了。处理sg值的时候有两种方法,一种是每次每个局面单独求一遍,递归出它的后继游戏的sg的值,然后它的sg值就是不包含后继状态sg值的最小非负整数,还有一种是对于一个S集合求所有状态的sg值一次求出来,然后打表,那么要求的时候就可以直接用了。

    代码一:递归求sg的值

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define pi acos(-1.0)
    int mex[110],sg[10010],s[110],n,m;
    int get_sg(int num)
    {
        int i,j;
        if(sg[num]!=-1)return sg[num];
        bool mex[110]; //mex的大小和S集合的大小差不多
        memset(mex,false,sizeof(mex));
        for(i=1;i<=n;i++){
            if(s[i]<=num){
                mex[get_sg(num-s[i])]=true;
            }
            else break;
        }
        for(i=0;;i++){
            if(!mex[i]){
                sg[num]=i;return i;
            }
        }
    }
    int main()
    {
        int i,j,l,c,num;
        while(scanf("%d",&n)!=EOF && n!=0)
        {
            for(i=1;i<=n;i++){
                scanf("%d",&s[i]);
            }
            sort(s+1,s+1+n); //这里要排序,因为get_sg函数中进行到s[i]>num就break了。
            memset(sg,-1,sizeof(sg));
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d",&l);
                num=0;
                for(i=1;i<=l;i++){
                    scanf("%d",&c);
                    num=num^get_sg(c);
                }
                if(num==0)printf("L");
                else printf("W");
            }
            printf("
    ");
        }
        return 0;
    }
    

    代码二:先打表求出所有局面的sg函数值。
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define pi acos(-1.0)
    int mex[110],sg[10010],s[110],n,m;
    void get_sg( )
    {
        int i,j;
        sg[0]=0;
        for(i=1;i<=10000;i++){
            memset(mex,0,sizeof(mex));
            for(j=1;j<=n;j++){
                if(s[j]<=i){
                    mex[sg[i-s[j] ] ]=1;
                }
                else break;
            }
            for(j=0;;j++){
                if(!mex[j]){
                    sg[i]=j;break;
                }
            }
    
        }
    
    
    
    
    }
    
    
    int main()
    {
        int i,j,l,c,num;
        while(scanf("%d",&n)!=EOF && n!=0)
        {
            for(i=1;i<=n;i++){
                scanf("%d",&s[i]);
            }
            sort(s+1,s+1+n);
            get_sg();
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d",&l);
                num=0;
                for(i=1;i<=l;i++){
                    scanf("%d",&c);
                    num=num^sg[c];
                }
                if(num==0)printf("L");
                else printf("W");
            }
            printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    servlet.txt笔记
    用数组实现集合的功能
    用父类声明的变量和用接口声明的变量的区别
    DHTML_____document对象的方法
    DHTML_____window对象的事件
    DHTML_____window对象属性
    DHTML_____window对象方法
    DHTML_____如何编写事件处理程序
    常用点击事件(鼠标、光标、键盘、body)
    鼠标滑动显示不同页面的效果——————获取鼠标相对于整个页面的坐标
  • 原文地址:https://www.cnblogs.com/herumw/p/9464584.html
Copyright © 2020-2023  润新知