📜  在Nim-Game中寻找获胜者

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

您会得到n个元素的数组A []。有两名球员爱丽丝和鲍勃。玩家可以从数组中选择任何元素并将其删除。如果在删除所选元素后,所有其余元素的按位XOR等于0,则该播放器松动。这个问题是nim游戏的变种。
注意:每个玩家交替玩游戏。找出赢家,如果两个球员都发挥最佳。爱丽丝首先开始游戏。如果数组中的一个元素将其值视为数组的XOR。
例子 :

让我们逐步开始解决方案。对于数组和该游戏的XOR,我们共有三个选项。

  1. 数组的XOR已经为0:在这种情况下,Alice将无法移动,因此Alice是获胜者。
  2. 数组的XOR不为零:现在,在这种情况下,我们有两个选择,数组的大小将为奇数或偶数。
    • 案例A:如果数组大小为奇数,则可以肯定的是Bob会赢得比赛。
    • 案例B:如果数组大小为偶数,则爱丽丝将赢得比赛。

以上结论可以借助数学归纳法得到证明。
令A [] = {1},即数组的大小为奇数,数组的XOR不为零:在这种情况下,爱丽丝可以选择元素1,然后A []将为空,因此数组的XOR可以视为零。结果鲍勃成为获胜者。
令数组的大小为偶数,而数组的XOR为非零。现在我们可以证明Alice总是可以找到要删除的元素,从而使数组剩余元素的XOR为非零。
为了证明这一点,让我们从矛盾出发,即假设您应该选择其余元素的XOR的任何元素都必须为零。
因此,让A1 Xor A2 Xor…An = X且n为偶数。
根据我们的矛盾假设,对于1 <= i <= n,Ai Xor X = 0。
计算所有X个Xor Ai的XOR(即n个方程),
在对所有n个方程进行XOR运算后,我们得到X Xor X…Xor X(n次)= 0,因为N是偶数。
现在,我们也有A1 Xor A2 Xor。An = 0,但我们知道A1 Xor A2…Xor =X。这意味着我们在偶数大小的数组中至少有一个元素,这样,在去除非元素中其余元素的XOR后,零。
令数组的大小为偶数,而数组的XOR为非零。爱丽丝不能删除元素Ai使得剩余数的异或为零,因为那样会使鲍勃获胜。现在,考虑剩余的N?1数的异或为非零的另一种情况。从归纳假设中我们知道N?1是偶数,我们可以说当前移动之后的位置将成为Bob的获胜位置。因此,对于爱丽丝来说,这是一个失败的职位。

int res = 0;
for (int i = 1; i <= N; i++) {
    res ^= a[i];
}

if (res == 0)
    return "ALice";
if (N%2 == 0)
    return "Alice";
else
    return "Bob";
C++
// CPP to find nim-game winner
#include 
using namespace std;
 
// function to find winner of NIM-game
string findWinner(int A[], int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res ^= A[i];
 
    // case when Alice is winner
    if (res == 0 || n % 2 == 0)
        return "Alice";
 
    // when Bob is winner
    else
        return "Bob";
}
 
// driver program
int main()
{
    int A[] = { 1, 4, 3, 5 };
    int n = siseof(A) / sizeof(A[0]);
    cout << "Winner = " << findWinner(A, n);
    return 0;
}


Java
// Java to find nim-game winner
class GFG {
     
    // function to find winner of NIM-game
    static String findWinner(int A[], int n)
    {
        int res = 0;
         
        for (int i = 0; i < n; i++)
            res ^= A[i];
     
        // case when Alice is winner
        if (res == 0 || n % 2 == 0)
            return "Alice";
     
        // when Bob is winner
        else
            return "Bob";
    }
     
    //Driver code
    public static void main (String[] args)
    {
        int A[] = { 1, 4, 3, 5 };
        int n =A.length;
         
        System.out.print("Winner = "
                    + findWinner(A, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find nim-game winner
 
# Function to find winner of NIM-game
def findWinner(A, n):
 
    res = 0
    for i in range(n):
        res ^= A[i]
 
    # case when Alice is winner
    if (res == 0 or n % 2 == 0):
        return "Alice"
 
    # when Bob is winner
    else:
        return "Bob"
 
# Driver code
A = [ 1, 4, 3, 5 ]
n = len(A)
print("Winner = ", findWinner(A, n))
 
# This code is contributed by Anant Agarwal.


C#
// C# to find nim-game winner
using System;
 
class GFG {
     
    // function to find winner of NIM-game
    static String findWinner(int []A, int n)
    {
        int res = 0;
         
        for (int i = 0; i < n; i++)
            res ^= A[i];
     
        // case when Alice is winner
        if (res == 0 || n % 2 == 0)
            return "Alice";
     
        // when Bob is winner
        else
            return "Bob";
    }
     
    //Driver code
    public static void Main ()
    {
        int []A = { 1, 4, 3, 5 };
        int n =A.Length;
         
        Console.WriteLine("Winner = "
                    + findWinner(A, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

Winner = Alice