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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200;
void findStates( int position[])
{
position[0] = 0;
position[1] = 1;
position[2] = 0;
position[3] = 1;
position[4] = 1;
position[5] = 1;
position[6] = 1;
position[7] = 0;
for ( int i = 8; i <= MAX; i++) {
if (!position[i - 1] || !position[i - 3]
|| !position[i - 4])
position[i] = 1;
else
position[i] = 0;
}
}
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
class GFG {
static final int MAX = 200 ;
static void findStates( int position[])
{
position[ 0 ] = 0 ;
position[ 1 ] = 1 ;
position[ 2 ] = 0 ;
position[ 3 ] = 1 ;
position[ 4 ] = 1 ;
position[ 5 ] = 1 ;
position[ 6 ] = 1 ;
position[ 7 ] = 0 ;
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 ;
}
}
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" );
}
}
|
Python3
MAX = 200
def findStates(position):
position[ 0 ] = 0 ;
position[ 1 ] = 1 ;
position[ 2 ] = 0 ;
position[ 3 ] = 1 ;
position[ 4 ] = 1 ;
position[ 5 ] = 1 ;
position[ 6 ] = 1 ;
position[ 7 ] = 0
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 ;
N = 100
position = [ 0 ] * ( MAX + 1 )
findStates(position)
if (position[N] = = 1 ):
print ( "Player 1" )
else :
print ( "Player 2" )
|
C#
using System;
class GFG {
static int MAX = 200;
static void findStates( int []position)
{
position[0] = 0;
position[1] = 1;
position[2] = 0;
position[3] = 1;
position[4] = 1;
position[5] = 1;
position[6] = 1;
position[7] = 0;
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;
}
}
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" );
}
}
|
PHP
<?php
$MAX = 200;
function findStates( $position )
{
global $MAX ;
$position [0] = 0;
$position [1] = 1;
$position [2] = 0;
$position [3] = 1;
$position [4] = 1;
$position [5] = 1;
$position [6] = 1;
$position [7] = 0;
for ( $i = 8; $i <= $MAX ; $i ++) {
if (! $position [ $i - 1] || ! $position [ $i - 3]
|| ! $position [ $i - 4])
$position [ $i ] = 1;
else
$position [ $i ] = 0;
}
}
$N = 100;
$position [ $MAX ] = array (0);
findStates( $position );
if ( $position == 1)
echo "Player 1" ;
else
echo "Player 2" ;
#This code is contributed by ajit
?>
|
Javascript
<script>
let MAX = 200;
function findStates(position)
{
position[0] = 0;
position[1] = 1;
position[2] = 0;
position[3] = 1;
position[4] = 1;
position[5] = 1;
position[6] = 1;
position[7] = 0;
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;
}
}
let N = 100;
let position = [];
findStates(position);
if (position[N] == 1)
document.write( "Player 1" );
else
document.write( "Player 2" );
</script>
|
Output:
Player 2
Time complexity : O(MAX)
Space Complexity : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jun, 2022
Like Article
Save Article