Game of Chocolates | Wythoff’s Game
Bunty and Dolly are playing a game, described as follows:
There are two boxes having A and B number of chocolates respectively. Both can eat L (L ≥ 1) chocolates from any one box or L chocolates from both boxes in one move. They play the game alternatively and the last one to eat the chocolate will be the winner.
You have to help Bunty in deciding who should play first such that Dolly is always the winner. Assume that both players play optimally.
Note: This game is also known as Wythoff’s Game.
Examples:
Input: A = 1 and B = 2
Output: Bunty
Explanation: If Bunty starts first, all the possible states after Bunty eats chocolates are (0, 2), (1, 1), (1, 0). These are all wining states for DollyInput: A = 1 and B = 3
Output: Dolly
Approach: The problem can be solved based on the following idea:
Any state can be uniquely identified using two integers (n, m) which are the number of chocolates in the boxes. Now each state can be classified into two categories: cold state (the player whose turn it is will lose) and hot state (the player whose turn it is will win). The classification can be done as follows:
- (0, 0) is a cold position
- Any state from which a cold state can be reached in a single move is a hot state.
- The state from which any possible move leads to a hot state is a cold state.
The task is to check if the given state (A, B) is a hot state or cold state and based on that the one to start will be Dolly or Bunty respectively.
Follow the below steps to solve the problem:
- If A > B, then swap A and B, so that, B is always greater than or equal to A.
- Initialize k = B – A and d = 1 + sqrt(5).
- Set, d = d/2, and again d = d*k.
- Make a variable c = d after typecasting it into int.
- Now, if a and c are equal then return 0 else return 1.
- If the answer is 0 then print “Dolly” else print “Bunty”.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to decide who should play first bool game( int a, int b) { // Swap the value if (a > b) swap(a, b); int k = b - a; long double d = 1 + sqrt (5); d /= 2; d *= k; int c = ( int )d; // Return answer return (a == c) ? 0 : 1; } // Driver Code int main() { int A = 1, B = 2; // Function call cout << (game(A, B) ? "Dolly" : "Bunty" ) << endl; return 0; } |
Java
// Java code to implement the approach import java.util.*; class GFG { // Function to decide who should play first static boolean game( int a, int b) { // Swap the value if (a > b) { int t=a; a=b; b=t; } int k = b - a; long d = 1 + ( int )Math.sqrt( 5 ); d /= 2 ; d *= k; int c = ( int )d; // Return answer return (a == c) ? false : true ; } // Driver Code public static void main (String[] args) { int A = 1 , B = 2 ; // Function call System.out.println((game(A, B) ? "Dolly" : "Bunty" )); } } // This code is contributed by sanjoy_62. |
Python3
# Python code to implement the approach import math # Function to decide who should play first def game(a, b): # Swap the value if a > b: t = a a = b b = t k = b - a d = 1 + math.sqrt( 5 ) d = d / / 2 d = d * k c = int (d) # Return answer return False if a = = c else True # Test function A = 1 B = 2 print ( "Dolly" if game(A, B) else "Bunty" ) # This code is contributed by lokeshmvs21. |
C#
// C# code to implement the approach using System; public class GFG { // Function to decide who should play first static bool game( int a, int b) { // Swap the value if (a > b) { int t = a; a = b; b = t; } int k = b - a; long d = 1 + ( int )Math.Sqrt(5); d /= 2; d *= k; int c = ( int )d; // Return answer return (a == c) ? false : true ; } static public void Main() { // Code int A = 1, B = 2; // Function call Console.WriteLine((game(A, B) ? "Dolly" : "Bunty" )); } } // This code is contributed by lokesh. |
Javascript
// JavaScript code to implement the approach function game(a, b) { // Swap the value if (a > b) { let temp = a; a = b; b = temp; } let k = b - a; let d = 1 + Math.sqrt(5); d /= 2; d *= k; let c = Math.floor(d); // Return answer return (a == c) ? false : true ; } let A = 1; let B = 2; // Function call console.log(game(A, B) ? "Dolly" : "Bunty" ); // This code is contributed by ishankhandelwals. |
Bunty
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...