Предпоставки: Grundy числа/числа и Mex
Вече видяхме в сет 2 (https://www.geeksforgeeks.org/dsa/combinatorial-game-toory-set-2-game-nim/), че можем да намерим кой печели в игра на NIM, без всъщност да играем играта.
Да предположим, че променяме малко класическата игра на NIM. Този път всеки играч може да премахне само 1 2 или 3 камъни (а не всякакъв брой камъни, както в класическата игра на NIM). Можем ли да предвидим кой ще спечели?
Да, можем да предвидим победителя, използвайки Sprague-Grundy теорема.
Какво е Sprague-Grundy Theorem?
Да предположим, че има композитна игра (повече от една под-мачове), съставена от N под игрите и двама играчи A и B. След това Sprague-Grundy теорема казва, че ако и A, и B играят оптимално (т.е. те не правят грешки), тогава играчът, който започва първо, е гарантиран, че печели, ако Xor на грубите числа на позицията във всяка под-геймс в началото на играта е незър. В противен случай, ако XOR оцени на нула, тогава играч А ще загуби определено, независимо какво.
Как да приложим Sprague Grundy Theorem?
Можем да приложим Sprague-Grundy теорема във всяка безпристрастна игра и го решете. Основните стъпки са изброени, както следва:
- Разбийте композитната игра на под игрите.
- След това за всяка под-игра изчислете грундито число на тази позиция.
- След това изчислете XOR на всички изчислени Grundy числа.
- Ако стойността на XOR е не-нулева, тогава играчът, който ще направи Turn (First Player), ще спечели друго, той е предопределен да загуби, независимо какво.
Примерна игра: Играта започва с 3 купчини с 3 4 и 5 камъка и играчът за преместване може да отнеме всеки положителен брой камъни до 3 само от която и да е от купчините [при условие че купчината има толкова много камъни]. Последният играч, който се движи, печели. Кой играч печели играта, ако и двамата играчи играят оптимално?
Как да разбера кой ще спечели, като приложи теорема на Sprague-Grundy?
Както можем да видим, че тази игра сама е съставена от няколко под-игри.
Първа стъпка: Под игрите могат да се считат за всяка купчина.
Втора стъпка: От тази по -долу виждаме, че
Grundy(3) = 3 Grundy(4) = 0 Grundy(5) = 1
Вече видяхме как да изчислим грубите числа на тази игра в предишен статия.
Трета стъпка: XOR от 3 0 1 = 2
Четвърта стъпка: Тъй като XOR е ненулев номер, така че можем да кажем, че първият играч ще спечели.
По -долу е програмата, която реализира над 4 стъпки.
if и else в bashC++
/* Game Description- 'A game is played between two players and there are N piles of stones such that each pile has certain number of stones. On his/her turn a player selects a pile and can take any non-zero number of stones upto 3 (i.e- 123) The player who cannot move is considered to lose the game (i.e. one who take the last stone is the winner). Can you find which player wins the game if both players play optimally (they don't make any mistake)? ' A Dynamic Programming approach to calculate Grundy Number and Mex and find the Winner using Sprague - Grundy Theorem. */ #include using namespace std; /* piles[] -> Array having the initial count of stones/coins in each piles before the game has started. n -> Number of piles Grundy[] -> Array having the Grundy Number corresponding to the initial position of each piles in the game The piles[] and Grundy[] are having 0-based indexing*/ #define PLAYER1 1 #define PLAYER2 2 // A Function to calculate Mex of all the values in that set int calculateMex(unordered_set<int> Set) { int Mex = 0; while (Set.find(Mex) != Set.end()) Mex++; return (Mex); } // A function to Compute Grundy Number of 'n' int calculateGrundy(int n int Grundy[]) { Grundy[0] = 0; Grundy[1] = 1; Grundy[2] = 2; Grundy[3] = 3; if (Grundy[n] != -1) return (Grundy[n]); unordered_set<int> Set; // A Hash Table for (int i=1; i<=3; i++) Set.insert (calculateGrundy (n-i Grundy)); // Store the result Grundy[n] = calculateMex (Set); return (Grundy[n]); } // A function to declare the winner of the game void declareWinner(int whoseTurn int piles[] int Grundy[] int n) { int xorValue = Grundy[piles[0]]; for (int i=1; i<=n-1; i++) xorValue = xorValue ^ Grundy[piles[i]]; if (xorValue != 0) { if (whoseTurn == PLAYER1) printf('Player 1 will winn'); else printf('Player 2 will winn'); } else { if (whoseTurn == PLAYER1) printf('Player 2 will winn'); else printf('Player 1 will winn'); } return; } // Driver program to test above functions int main() { // Test Case 1 int piles[] = {3 4 5}; int n = sizeof(piles)/sizeof(piles[0]); // Find the maximum element int maximum = *max_element(piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy[maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER1 piles Grundy n); /* Test Case 2 int piles[] = {3 8 2}; int n = sizeof(piles)/sizeof(piles[0]); int maximum = *max_element (piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy [maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER2 piles Grundy n); */ return (0); }
Java import java.util.*; /* Game Description- 'A game is played between two players and there are N piles of stones such that each pile has certain number of stones. On his/her turn a player selects a pile and can take any non-zero number of stones upto 3 (i.e- 123) The player who cannot move is considered to lose the game (i.e. one who take the last stone is the winner). Can you find which player wins the game if both players play optimally (they don't make any mistake)? ' A Dynamic Programming approach to calculate Grundy Number and Mex and find the Winner using Sprague - Grundy Theorem. */ class GFG { /* piles[] -> Array having the initial count of stones/coins in each piles before the game has started. n -> Number of piles Grundy[] -> Array having the Grundy Number corresponding to the initial position of each piles in the game The piles[] and Grundy[] are having 0-based indexing*/ static int PLAYER1 = 1; static int PLAYER2 = 2; // A Function to calculate Mex of all the values in that set static int calculateMex(HashSet<Integer> Set) { int Mex = 0; while (Set.contains(Mex)) Mex++; return (Mex); } // A function to Compute Grundy Number of 'n' static int calculateGrundy(int n int Grundy[]) { Grundy[0] = 0; Grundy[1] = 1; Grundy[2] = 2; Grundy[3] = 3; if (Grundy[n] != -1) return (Grundy[n]); // A Hash Table HashSet<Integer> Set = new HashSet<Integer>(); for (int i = 1; i <= 3; i++) Set.add(calculateGrundy (n - i Grundy)); // Store the result Grundy[n] = calculateMex (Set); return (Grundy[n]); } // A function to declare the winner of the game static void declareWinner(int whoseTurn int piles[] int Grundy[] int n) { int xorValue = Grundy[piles[0]]; for (int i = 1; i <= n - 1; i++) xorValue = xorValue ^ Grundy[piles[i]]; if (xorValue != 0) { if (whoseTurn == PLAYER1) System.out.printf('Player 1 will winn'); else System.out.printf('Player 2 will winn'); } else { if (whoseTurn == PLAYER1) System.out.printf('Player 2 will winn'); else System.out.printf('Player 1 will winn'); } return; } // Driver code public static void main(String[] args) { // Test Case 1 int piles[] = {3 4 5}; int n = piles.length; // Find the maximum element int maximum = Arrays.stream(piles).max().getAsInt(); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy[] = new int[maximum + 1]; Arrays.fill(Grundy -1); // Calculate Grundy Value of piles[i] and store it for (int i = 0; i <= n - 1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER1 piles Grundy n); /* Test Case 2 int piles[] = {3 8 2}; int n = sizeof(piles)/sizeof(piles[0]); int maximum = *max_element (piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy [maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER2 piles Grundy n); */ } } // This code is contributed by PrinciRaj1992
Python3 ''' Game Description- 'A game is played between two players and there are N piles of stones such that each pile has certain number of stones. On his/her turn a player selects a pile and can take any non-zero number of stones upto 3 (i.e- 123) The player who cannot move is considered to lose the game (i.e. one who take the last stone is the winner). Can you find which player wins the game if both players play optimally (they don't make any mistake)? ' A Dynamic Programming approach to calculate Grundy Number and Mex and find the Winner using Sprague - Grundy Theorem. piles[] -> Array having the initial count of stones/coins in each piles before the game has started. n -> Number of piles Grundy[] -> Array having the Grundy Number corresponding to the initial position of each piles in the game The piles[] and Grundy[] are having 0-based indexing''' PLAYER1 = 1 PLAYER2 = 2 # A Function to calculate Mex of all # the values in that set def calculateMex(Set): Mex = 0; while (Mex in Set): Mex += 1 return (Mex) # A function to Compute Grundy Number of 'n' def calculateGrundy(n Grundy): Grundy[0] = 0 Grundy[1] = 1 Grundy[2] = 2 Grundy[3] = 3 if (Grundy[n] != -1): return (Grundy[n]) # A Hash Table Set = set() for i in range(1 4): Set.add(calculateGrundy(n - i Grundy)) # Store the result Grundy[n] = calculateMex(Set) return (Grundy[n]) # A function to declare the winner of the game def declareWinner(whoseTurn piles Grundy n): xorValue = Grundy[piles[0]]; for i in range(1 n): xorValue = (xorValue ^ Grundy[piles[i]]) if (xorValue != 0): if (whoseTurn == PLAYER1): print('Player 1 will winn'); else: print('Player 2 will winn'); else: if (whoseTurn == PLAYER1): print('Player 2 will winn'); else: print('Player 1 will winn'); # Driver code if __name__=='__main__': # Test Case 1 piles = [ 3 4 5 ] n = len(piles) # Find the maximum element maximum = max(piles) # An array to cache the sub-problems so that # re-computation of same sub-problems is avoided Grundy = [-1 for i in range(maximum + 1)]; # Calculate Grundy Value of piles[i] and store it for i in range(n): calculateGrundy(piles[i] Grundy); declareWinner(PLAYER1 piles Grundy n); ''' Test Case 2 int piles[] = {3 8 2}; int n = sizeof(piles)/sizeof(piles[0]); int maximum = *max_element (piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy [maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER2 piles Grundy n); ''' # This code is contributed by rutvik_56
C# using System; using System.Linq; using System.Collections.Generic; /* Game Description- 'A game is played between two players and there are N piles of stones such that each pile has certain number of stones. On his/her turn a player selects a pile and can take any non-zero number of stones upto 3 (i.e- 123) The player who cannot move is considered to lose the game (i.e. one who take the last stone is the winner). Can you find which player wins the game if both players play optimally (they don't make any mistake)? ' A Dynamic Programming approach to calculate Grundy Number and Mex and find the Winner using Sprague - Grundy Theorem. */ class GFG { /* piles[] -> Array having the initial count of stones/coins in each piles before the game has started. n -> Number of piles Grundy[] -> Array having the Grundy Number corresponding to the initial position of each piles in the game The piles[] and Grundy[] are having 0-based indexing*/ static int PLAYER1 = 1; //static int PLAYER2 = 2; // A Function to calculate Mex of all the values in that set static int calculateMex(HashSet<int> Set) { int Mex = 0; while (Set.Contains(Mex)) Mex++; return (Mex); } // A function to Compute Grundy Number of 'n' static int calculateGrundy(int n int []Grundy) { Grundy[0] = 0; Grundy[1] = 1; Grundy[2] = 2; Grundy[3] = 3; if (Grundy[n] != -1) return (Grundy[n]); // A Hash Table HashSet<int> Set = new HashSet<int>(); for (int i = 1; i <= 3; i++) Set.Add(calculateGrundy (n - i Grundy)); // Store the result Grundy[n] = calculateMex (Set); return (Grundy[n]); } // A function to declare the winner of the game static void declareWinner(int whoseTurn int []piles int []Grundy int n) { int xorValue = Grundy[piles[0]]; for (int i = 1; i <= n - 1; i++) xorValue = xorValue ^ Grundy[piles[i]]; if (xorValue != 0) { if (whoseTurn == PLAYER1) Console.Write('Player 1 will winn'); else Console.Write('Player 2 will winn'); } else { if (whoseTurn == PLAYER1) Console.Write('Player 2 will winn'); else Console.Write('Player 1 will winn'); } return; } // Driver code static void Main() { // Test Case 1 int []piles = {3 4 5}; int n = piles.Length; // Find the maximum element int maximum = piles.Max(); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int []Grundy = new int[maximum + 1]; Array.Fill(Grundy -1); // Calculate Grundy Value of piles[i] and store it for (int i = 0; i <= n - 1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER1 piles Grundy n); /* Test Case 2 int piles[] = {3 8 2}; int n = sizeof(piles)/sizeof(piles[0]); int maximum = *max_element (piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy [maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER2 piles Grundy n); */ } } // This code is contributed by mits
JavaScript <script> /* Game Description- 'A game is played between two players and there are N piles of stones such that each pile has certain number of stones. On his/her turn a player selects a pile and can take any non-zero number of stones upto 3 (i.e- 123) The player who cannot move is considered to lose the game (i.e. one who take the last stone is the winner). Can you find which player wins the game if both players play optimally (they don't make any mistake)? ' A Dynamic Programming approach to calculate Grundy Number and Mex and find the Winner using Sprague - Grundy Theorem. */ /* piles[] -> Array having the initial count of stones/coins in each piles before the game has started. n -> Number of piles Grundy[] -> Array having the Grundy Number corresponding to the initial position of each piles in the game The piles[] and Grundy[] are having 0-based indexing*/ let PLAYER1 = 1; let PLAYER2 = 2; // A Function to calculate Mex of all the values in that set function calculateMex(Set) { let Mex = 0; while (Set.has(Mex)) Mex++; return (Mex); } // A function to Compute Grundy Number of 'n' function calculateGrundy(nGrundy) { Grundy[0] = 0; Grundy[1] = 1; Grundy[2] = 2; Grundy[3] = 3; if (Grundy[n] != -1) return (Grundy[n]); // A Hash Table let Set = new Set(); for (let i = 1; i <= 3; i++) Set.add(calculateGrundy (n - i Grundy)); // Store the result Grundy[n] = calculateMex (Set); return (Grundy[n]); } // A function to declare the winner of the game function declareWinner(whoseTurnpilesGrundyn) { let xorValue = Grundy[piles[0]]; for (let i = 1; i <= n - 1; i++) xorValue = xorValue ^ Grundy[piles[i]]; if (xorValue != 0) { if (whoseTurn == PLAYER1) document.write('Player 1 will win
'); else document.write('Player 2 will win
'); } else { if (whoseTurn == PLAYER1) document.write('Player 2 will win
'); else document.write('Player 1 will win
'); } return; } // Driver code // Test Case 1 let piles = [3 4 5]; let n = piles.length; // Find the maximum element let maximum = Math.max(...piles) // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided let Grundy = new Array(maximum + 1); for(let i=0;i<maximum+1;i++) Grundy[i]=0; // Calculate Grundy Value of piles[i] and store it for (let i = 0; i <= n - 1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER1 piles Grundy n); /* Test Case 2 int piles[] = {3 8 2}; int n = sizeof(piles)/sizeof(piles[0]); int maximum = *max_element (piles piles + n); // An array to cache the sub-problems so that // re-computation of same sub-problems is avoided int Grundy [maximum + 1]; memset(Grundy -1 sizeof (Grundy)); // Calculate Grundy Value of piles[i] and store it for (int i=0; i<=n-1; i++) calculateGrundy(piles[i] Grundy); declareWinner(PLAYER2 piles Grundy n); */ // This code is contributed by avanitrachhadiya2155 </script>
Резултат:
Player 1 will win
Сложност на времето: O (n^2), където n е максималният брой камъни в купчина.
Космическа сложност: O (n) Тъй като масивът на Grundy се използва за съхраняване на резултатите от подпроблемите, за да се избегнат излишни изчисления и е необходимо O (n) пространство.
Референции:
https://en.wikipedia.org/wiki/Sprague%E2%80%93Grundy_theorem
Упражнение към читателите: Помислете за играта по -долу.
Играта се играе от двама играчи с n цели числа A1 A2 .. an. На свой ред играчът избира цяло число, което го разделя на 2 3 или 6 и след това взема пода. Ако цяло число стане 0, то се отстранява. Последният играч, който се движи, печели. Кой играч печели играта, ако и двамата играчи играят оптимално?
Съвет: Вижте примера 3 на предишен статия.