Prerequisites:
Sprague Grundy theorem
Grundy Numbers
Nim is a famous game in which two players take turns removing items from distinct piles. During each turn, a player must remove one or more items from a single, non-empty pile. The winner of the game is whichever player removes the last item from the last non-empty pile.
Now, For each non-empty pile, either player can remove zero items from that pile and have it count as their move; however, this move can only be performed once per pile by either player.
Given the number of items in each pile, determine who will win the game; Player 1 or player 2. If player 1 starts the game and both plays optimally.
Examples:
Input : 3 [18, 47, 34]
Output : Player 2 wins
G = g(18)^g(47)^g(34) = (17)^(48)^(33) = 0
Grundy number(G), for this game is zero.
Player 2 wins.
Input : 3 [32, 49, 58]
Output : Player 1 wins
G = g(31)^g(50)^g(57) = (17)^(48)^(33) = 20
Grundy number(G), for this game is non-zero.
Player 1 wins.
Approach:
Grundy number for each pile is calculated based on the number of stones.To compensate the zero move we will have to modify grundy values we used in standard nim game.
If pile size is odd; grundy number is size+1 and
if pile size is even; grundy number is size-1.
We XOR all the grundy number values to check if final Grundy number(G) of game is non zero or not to decide who is winner of game.
Explanation:
Grundy number of a state is the smallest positive integer that cannot be reached in one valid move.
So, we need to calculate mex value for each n, bottom up wise so that we can induce the grundy number for each n. where n is the pile size.
Winning state: A tuple of values from where the current player will win the game no matter what opponent does. (If G!=0)
Losing state: A tuple of values from where the current player will lose the game no matter what opponent does. (If G=0)
For a given pile size n, we have two states:
(1) n with no zero moves available,
grundy number will same as standard nim game.
(2) n with zero moves available, we can
reach above state and other states with
zero moves remaining.
For, n = 0, g(0) = 0, empty pile
For, n = 1, we can reach two states:
(1) n = 0 (zero move not used)
(2) n = 1 (zero move used)
Therefore, g(1) = mex{0, 1} which implies
that g(1)=2.
For, n = 2, we can reach :
(1) n = 0 (zero move not used) state
because this is a valid move.
(2) n = 1 (zero move not used) is a valid
move, whose grundy number is 2.
Therefore, g(2) = mex{0,2} which implies
that g(2)=1.
note that n=1 (zero move used) is not a valid
move.
If we try to build a solution bottom-up
like this, it turns out that if n is even,
the grundy number is n - 1 and when it is odd,
the grundy is n + 1.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int p[], int n)
{
int G = 0;
for ( int i = 0; i < n; i++) {
if (p[i] & 1)
G ^= (p[i] + 1);
else
G ^= (p[i] - 1);
}
return G;
}
int main()
{
int n = 3;
int p[3] = { 32, 49, 58 };
int res = solve(p, n);
if (res == 0)
cout << "Player 2 wins" ;
else
cout << "Player 1 wins" ;
return 0;
}
|
Java
class GFG {
static int solve( int p[], int n)
{
int G = 0 ;
for ( int i = 0 ; i < n; i++) {
if (p[i]% 2 != 0 )
G ^= (p[i] + 1 );
else
G ^= (p[i] - 1 );
}
return G;
}
public static void main (String[] args)
{
int n = 3 ;
int p[] = { 32 , 49 , 58 };
int res = solve(p, n);
if (res == 0 )
System.out.print( "Player 2 wins" );
else
System.out.print( "Player 1 wins" );
}
}
|
Python3
def solve(p, n):
G = 0
for i in range (n):
if (p[i] % 2 ! = 0 ):
G ^ = (p[i] + 1 )
else :
G ^ = (p[i] - 1 )
return G
n = 3
p = [ 32 , 49 , 58 ]
res = solve(p, n)
if (res = = 0 ):
print ( "Player 2 wins" )
else :
print ( "Player 1 wins" )
|
C#
using System;
class GFG {
static int solve( int [] p, int n)
{
int G = 0;
for ( int i = 0; i < n; i++) {
if (p[i] % 2 != 0)
G ^= (p[i] + 1);
else
G ^= (p[i] - 1);
}
return G;
}
public static void Main()
{
int n = 3;
int [] p = { 32, 49, 58 };
int res = solve(p, n);
if (res == 0)
Console.WriteLine( "Player 2 wins" );
else
Console.WriteLine( "Player 1 wins" );
}
}
|
PHP
<?php
function solve( $p , $n )
{
$G = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $p [ $i ] & 1)
$G ^= ( $p [ $i ] + 1);
else
$G ^= ( $p [ $i ] - 1);
}
return $G ;
}
$n = 3;
$p = array ( 32, 49, 58 );
$res = solve( $p , $n );
if ( $res == 0)
echo "Player 2 wins" ;
else
echo "Player 1 wins" ;
?>
|
Javascript
<script>
function solve(p, n)
{
let G = 0;
for (let i = 0; i < n; i++)
{
if (p[i] % 2 != 0)
G ^= (p[i] + 1);
else
G ^= (p[i] - 1);
}
return G;
}
let n = 3;
let p = [ 32, 49, 58 ];
let res = solve(p, n);
if (res == 0)
document.write( "Player 2 wins" );
else
document.write( "Player 1 wins" );
</script>
|
Output:
Player 1 wins
Time Complexity: O(n)
Space Complexity: O(n)
Last Updated :
13 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...