Poker Nim

Poker Nim is another -player game that's a simple variation on a Nim game. The rules of the games are as follows:

  • The game starts with n piles of chips indexed from 0 to n-1. Each pile  (where ) has  chips.
  • The players move in alternating turns. During each move, the current player must perform either of the following actions:

    • Remove one or more chips from a single pile.
    • Add one or more chips to a single pile.

    At least 1 chip must be added or removed during each turn.

  • To ensure that the game ends in finite time, a player cannot add chips to any pile more than k times.
  • The player who removes the last chip wins the game.

Given the values of , and the numbers of chips in each of the  piles, determine whether the person who wins the game is the first or second person to move. Assume both players move optimally.

Example:

Input:  n = 3, k = 5, c = {2, 1, 3}
Output: Second

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string pokerNim(int kvector<intc)
{
    int res = 0;
    for (int i = 0i < c.size(); i++)
    {
        res ^= c[i];
    }
    if (res > 0)
    {
        return "First";
    }
    else
        return "Second";
}

int main()
{

    int n = 3k = 5;
    vector<intc = {213};

    cout << pokerNim(kc<< "\n";

    return 0;
}


No comments:

Post a Comment