📜  在 NIM 游戏中以最佳方式进行第一步的方法数

📅  最后修改于: 2021-09-24 04:57:08             🧑  作者: Mango

两个玩家AB正在互相玩 NIM 游戏。两者都发挥最佳。玩家A开始游戏。任务是找到打1移动方式编号为确保作为一个成功的策略如果可能的话,否则输出-1。
例子:

方法:

  1. 首先通过对所有数组元素进行异或来检查谁将赢得比赛,如果 XOR 为零,那么无论 A 玩得如何最佳,A 总是输。如果 XOR 非零,则转到步骤 2。
  2. 我们将检查每一堆是否可以从那堆中取出一些硬币,以便在移动之后,所有数组元素的 XOR 将为零。因此,对于所有的堆,我们将对数组的所有剩余元素逐一进行异或,并检查 XOR 值是否大于堆中的硬币数量。如果是这样,则无法使用此堆进行第一步,因为我们只能在一次移动中从堆中取出硬币,而不能添加硬币。否则,我们将增加方式的数量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to return the XOR
// of all the array elements
int xorArray(int arr[], int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    return res;
}
 
// Function to return the count of ways
// to play the first move optimally
int getTotalWays(int arr[], int n)
{
 
    // XOR of all the array elements
    int xorArr = xorArray(arr, n);
 
    // The player making the first move
    // can't win the game no matter
    // how optimally he plays
    if (xorArr == 0)
        return -1;
 
    // Initialised with zero
    int numberOfWays = 0;
 
    for (int i = 0; i < n; i++) {
 
        // requiredCoins is the number of coins
        // the player making the move must leave
        // in the current pile in order to play optimally
        int requiredCoins = xorArr ^ arr[i];
 
        // If requiredCoins is less than the current
        // amount of coins in the current pile
        // then only the player can make an optimal move
        if (requiredCoins < arr[i])
            numberOfWays++;
    }
 
    return numberOfWays;
}
 
// Driver code
int main()
{
 
    // Coins in each pile
    int arr[] = { 3, 4, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << getTotalWays(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Utility function to return the XOR
// of all the array elements
static int xorArray(int arr[], int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    return res;
}
 
// Function to return the count of ways
// to play the first move optimally
static int getTotalWays(int arr[], int n)
{
 
    // XOR of all the array elements
    int xorArr = xorArray(arr, n);
 
    // The player making the first move
    // can't win the game no matter
    // how optimally he plays
    if (xorArr == 0)
        return -1;
 
    // Initialised with zero
    int numberOfWays = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // requiredCoins is the number of coins
        // the player making the move must leave
        // in the current pile in order to play optimally
        int requiredCoins = xorArr ^ arr[i];
 
        // If requiredCoins is less than the current
        // amount of coins in the current pile
        // then only the player can make an optimal move
        if (requiredCoins < arr[i])
            numberOfWays++;
    }
    return numberOfWays;
}
 
// Driver code
public static void main(String[] args)
{
 
    // Coins in each pile
    int arr[] = { 3, 4, 4, 2 };
    int n =arr.length;
 
    System.out.println(getTotalWays(arr, n));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
 
# Utility function to return the
# XOR of all the array elements
def xorArray(arr, n):
 
    res = 0
    for i in range(0, n):
        res = res ^ arr[i]
 
    return res
 
# Function to return the count of ways
# to play the first move optimally
def getTotalWays(arr, n):
 
    # XOR of all the array elements
    xorArr = xorArray(arr, n)
 
    # The player making the first move
    # can't win the game no matter
    # how optimally he plays
    if xorArr == 0:
        return -1
 
    # Initialised with zero
    numberOfWays = 0
 
    for i in range(0, n):
 
        # requiredCoins is the number of coins the
        # player making the move must leave in the
        # current pile in order to play optimally
        requiredCoins = xorArr ^ arr[i]
 
        # If requiredCoins is less than the current
        # amount of coins in the current pile
        # then only the player can make an optimal move
        if requiredCoins < arr[i]:
            numberOfWays += 1
 
    return numberOfWays
 
# Driver code
if __name__ == "__main__":
 
    # Coins in each pile
    arr = [3, 4, 4, 2]
    n = len(arr)
 
    print(getTotalWays(arr, n))
     
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Utility function to return the XOR
// of all the array elements
static int xorArray(int []arr, int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    return res;
}
 
// Function to return the count of ways
// to play the first move optimally
static int getTotalWays(int []arr, int n)
{
 
    // XOR of all the array elements
    int xorArr = xorArray(arr, n);
 
    // The player making the first move
    // can't win the game no matter
    // how optimally he plays
    if (xorArr == 0)
        return -1;
 
    // Initialised with zero
    int numberOfWays = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // requiredCoins is the number of coins
        // the player making the move must leave
        // in the current pile in order to play optimally
        int requiredCoins = xorArr ^ arr[i];
 
        // If requiredCoins is less than the current
        // amount of coins in the current pile
        // then only the player can make an optimal move
        if (requiredCoins < arr[i])
            numberOfWays++;
    }
    return numberOfWays;
}
 
// Driver code
static public void Main ()
{
 
    // Coins in each pile
    int []arr = { 3, 4, 4, 2 };
    int n = arr.Length;
     
    Console.Write(getTotalWays(arr, n));
}
}
 
// This code is contributed by ajit.


PHP


Javascript


输出:
1

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程