Sort by

recency

|

19 Discussions

|

  • + 0 comments

    IIXXIIIIII if my turn is first how can i win if i and my friend knockdown all two pins which are adjacent to each other

    II - my turn XX -skipped II - his turn II - myturn II -his turn

  • + 1 comment

    Here is Bowling Pins problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-bowling-pins-problem-solution.html

  • + 0 comments

    Hello! I've left my Java 8 solution here in case anyone has trouble.

    Just keep in mind: - GrundyNumbers. - Minimum Excludant. - GrundyNumber of multiple games is the XOR of the grundy numbers of the games. - You only have to evaluate a state once.

  • + 1 comment

    Hey, I'm trying to solve this problem. I studied the combinational game theory but I find something confusing. If i have three pins. III mex{1,2} = 0 means LOSE. But I can knock down the middle pin and it will be IXI for the next player he/she can only knock either the left or right so I will it will be a WIN. Am I implementing the game theory wrong or...?

  • [deleted]
    + 0 comments
    int mex(set<int>mset)
    {
        int m=0;
        while(mset.find(m)!=mset.end())
        m++;
        return m;
    }
    
    
    int grundy(string &s,int ci)
    {
        //cout<<s<<endl;
        if(dp.find(s)!=dp.end())
        {
            return dp[s];
        }
        if (ci==0) {
            dp[s] = 0;
            string t = s;
            //cout<<s<<endl;
            //reverse(s.begin(), s.end());
            //dp[t] = 0;
            return 0;
            }
        if (ci == 1) {
          dp[s] = 1;
          string t = s;
          //cout<<s<<endl;
          //reverse(s.begin(), s.end());
          //dp[t] = 1;
          return 1;
        }
        int n=s.length();
        set<int>mset;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='X')
                continue;
            if (s[i] == 'I') {
              s[i] = 'X';
              int ans = grundy(s,ci-1);
              mset.insert(ans);
              s[i] = 'I';
              //cout<<"BACK TO "<<s<<endl;
            }
    
            if((i+1)<n && s[i]=='I' && s[i+1]=='I')
            {
                s[i]='X';
                s[i+1]='X';
                int ans=grundy(s,ci-2);
                mset.insert(ans);
                s[i]='I';
                s[i+1]='I';
            }
            
        }
        dp[s]=mex(mset);
        return dp[s];
    }
    
    
    
    string isWinning(int n, string config) {
        /*
         * Return WIN or LOSE depending on whether you will win
         */
        int c=count(config.begin(),config.end(),'I');
        bool ans=(bool)grundy(config,c);
        vector<string>arr(2);
        arr[0]="LOSE";
        arr[1]="WIN";
        return arr[ans];
    }
    

    I am using a game theory based approach here but still getting timed out. Could someone please help ?