两个玩家A和B互相玩NIM游戏。两者都发挥最佳。玩家A开始游戏。任务是找到打1个移动方式的编号为确保作为一个成功的策略如果可能的话,否则输出-1。
例子:
Input: arr[] = {1, 2, 3}
Output: -1
There is no winning strategy for A no matter how optimally he plays.
Input: arr[] = {2, 4, 5}
Output: 1
In order to play optimally, A will pick one coin from first pile and that’s the only optimal move.
方法:
- 首先,如果XOR为零,则通过对所有数组元素进行XOR来检查谁会赢得比赛,那么无论A发挥多么理想,A总是会输掉。如果XOR不为零,请转到步骤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
输出:
1
时间复杂度: O(N)