Game of N stones where each player can remove 1, 3 or 4

3

Two players are playing a game with n stones where player 1 always plays first. The two players move in alternating turns and plays optimally. In a single move a player can remove either 1, 3 or 4 stones from the pile of stones. If a player is unable to make a move then that player loses the game. Given the number of stones where n is less than equal to 200, find and print the name of the winner.

Examples:

Input : 4
Output : player 1

Input : 7
Output : player 2

To solve this problem, we need to find each possible value of n as a winning or losing position. Since above game is one of the impartial combinatorial games, therefore the characterization of losing and winning position is valid.

The characteristic properties of winning and losing states are:

  • All terminal positions are losing positions.
  • From every winning position, there is atleast one move to a losing position.
  • From every losing position, every move is to a winning position.

If a player is able to make a move such that the next move is the losing state than the player is at winning state. Find the state of player 1, if player1 is in winning state then player 1 wins the game otherwise player 2 will win.

Consider the following base positions:

  1. position 0 is the losing state, if the number of stones is 0 than the player1 will unable to make a move therefore player1 loses.
  2. position 1 is the winning state, if the number of stones is 1 than the player1 will remove the stone and win the game.
  3. position 2 is the losing state, if the number of stones is 2 than the player1 will remove 1 stone and then player2 will remove the second stone and win the game.
  4. position 3 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove all the 3 stones
  5. position 4 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove all the 4 stones
  6. position 5 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove 3 stones leaving 2 stones, which is the losing state
  7. position 6 is the winning state, if the player is able to take a move such that the next move is the losing state than the player is at winning state, the palyer1 will remove 4 stones leaving 2 stones, which is the losing state
  8. position 7 is the losing state, if the number of stones is 7 than the player1 can remove 1, 3 or 4 stones which all leads to the losing state, therefore player1 will lose.

Below is the implementation of above approach:

// CPP program to find winner of
// the game of N stones
#include <bits/stdc++.h>
using namespace std;

const int MAX = 200;

// finds the winning and losing
// states for the 200 stones.
void findStates(int position[])
{
    // 0 means losing state
    // 1 means winning state
    position[0] = 0;
    position[1] = 1;
    position[2] = 0;
    position[3] = 1;
    position[4] = 1;
    position[5] = 1;
    position[6] = 1;
    position[7] = 0;

    // find states for other positions
    for (int i = 8; i <= MAX; i++) {
        if (!position[i - 1] || !position[i - 3]
            || !position[i - 4])
            position[i] = 1;
        else
            position[i] = 0;
    }
}

// driver function
int main()
{
    int N = 100;
    int position[MAX] = { 0 };

    findStates(position);

    if (position[N] == 1)
        cout << "Player 1";
    else
        cout << "Player 2";

    return 0;
}

Output:

Player 2

This article is contributed by ARSHPREET_SINGH. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.