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 at least 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, then 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:
- Position 0 is the losing state, if the number of stones is 0 than the player1 will be unable to make a move therefore player1 loses.
- Position 1 is the winning state, if the number of stones is 1 than the player1 will remove the stone and win the game.
- 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.
- 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
- 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
- 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
- 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
-
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;
} |
// Java program for the variation // in nim game class GFG {
static final int MAX = 200 ;
// finds the winning and losing
// states for the 200 stones.
static 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 ]!= 1 ||
position[i - 3 ]!= 1
|| position[i - 4 ]!= 1 )
position[i] = 1 ;
else
position[i] = 0 ;
}
}
//Driver code
public static void main (String[] args)
{
int N = 100 ;
int position[]= new int [MAX];
findStates(position);
if (position[N] == 1 )
System.out.print( "Player 1" );
else
System.out.print( "Player 2" );
}
} // This code is contributed by Anant Agarwal. |
# Python3 program to find winner of # the game of N stones MAX = 200
# finds the winning and losing # states for the 200 stones. def findStates(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 i in range ( 8 , MAX + 1 ):
if not (position[i - 1 ]) or not (position[i - 3 ]) or not (position[i - 4 ]):
position[i] = 1 ;
else :
position[i] = 0 ;
#driver function N = 100
position = [ 0 ] * ( MAX + 1 )
findStates(position) if (position[N] = = 1 ):
print ( "Player 1" )
else :
print ( "Player 2" )
# This code is contributed by # Smitha Dinesh Semwal |
// C# program for the variation // in nim game using System;
class GFG {
static int MAX = 200;
// finds the winning and losing
// states for the 200 stones.
static 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] != 1
|| position[i - 3] != 1
|| position[i - 4]!=1)
position[i] = 1;
else
position[i] = 0;
}
}
// Driver code
public static void Main ()
{
int N = 100;
int []position = new int [MAX];
findStates(position);
if (position[N] == 1)
Console.WriteLine( "Player 1" );
else
Console.WriteLine( "Player 2" );
}
} // This code is contributed by vt_m. |
<?php // PHP program to find winner of // the game of N stones $MAX = 200;
// finds the winning and losing // states for the 200 stones. function findStates( $position )
{ global $MAX ;
// 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 ( $i = 8; $i <= $MAX ; $i ++) {
if (! $position [ $i - 1] || ! $position [ $i - 3]
|| ! $position [ $i - 4])
$position [ $i ] = 1;
else
$position [ $i ] = 0;
}
} // driver function $N = 100;
$position [ $MAX ] = array (0);
findStates( $position );
if ( $position == 1)
echo "Player 1" ;
else
echo "Player 2" ;
#This code is contributed by ajit ?> |
<script> // Javascript program for the variation // in nim game let MAX = 200;
// finds the winning and losing
// states for the 200 stones.
function findStates(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 (let i = 8; i < MAX; i++)
{
if (position[i - 1] != 1
|| position[i - 3] != 1
|| position[i - 4]!=1)
position[i] = 1;
else
position[i] = 0;
}
}
// Driver code let N = 100;
let position = [];
findStates(position);
if (position[N] == 1)
document.write( "Player 1" );
else
document.write( "Player 2" );
// This code is contributed by code_hunt.
</script> |
Output:
Player 2
Time complexity : O(MAX)
Space Complexity : O(1)