📌  相关文章
📜  从给定的 N 堆中索引最少的非空堆中取出任意数量的石头,找出游戏的获胜者

📅  最后修改于: 2021-10-27 03:19:27             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,每个整数代表一堆石头的大小。任务是当两个玩家AB根据以下条件进行最佳游戏时,确定游戏的获胜者:

  • 玩家A总是开始游戏。
  • 在一次移动中,玩家可以从具有最小索引的第一个非空堆中移除任意数量的石头(至少 1 个)。
  • 第一个不能移动的玩家输掉游戏。

如果玩家 A 赢得游戏,则打印“ A” 。否则,打印“ B”

例子:

方法:这个想法是找出玩家应该遵循的最佳方式来赢得比赛。以下是两种最佳播放方式:

  • 对于除最后一个之外的所有堆,如果一堆中有K块石头,则当前玩家将只选择 ( K – 1 ) 块石头(仅当 K > 1 时),以便另一个玩家被迫选择剩余的1块石头。这保证了当前玩家有机会从下一堆捡石头并最终获胜。
  • 对于最后一叠,如果有K 个石头,则当前玩家将所有K 个石头都捡起来,这样其他玩家就没有机会捡石头了。这最终保证了当前玩家获胜。

请按照以下步骤解决此问题:

  1. 最初,假设玩家将从当前桩除去所有的石头,A将是赢家如果数组的大小是奇数B如果尺寸是均匀的。
  2. 现在,迭代范围[0, N – 2]并检查当前堆上的当前索引和石头数量,以确定哪个玩家将获胜。
  3. 遍历时,如果索引为偶数,A 将有机会捡到一块石头。否则,B 将有机会捡石头。
  4. 如果当前指数为偶数且棋子数大于1 ,则获胜者设置为B 。将获胜者更新为A
  5. 如果当前指数为奇数且棋子数超过1 ,则获胜者设置为A。然后,将获胜者更新为B
  6. 最后,打印游戏的获胜者。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the winner
// of game between A and B
void findWinner(int a[], int n)
{
    // win = 1 means B is winner
    // win = 0 means A is winner
    int win = 0;
 
    // If size is even, winner is B
    if (n % 2 == 0)
        win = 1;
 
    // If size is odd, winner is A
    else
        win = 0;
 
    for (int i = n - 2; i >= 0; i--) {
 
        // Stone will be removed by B
        if (i % 2 == 1) {
 
            // If B wants to win
 
            // B will take n-1 stones
            // from current pile having
            // n stones and force A to
            // pick 1 stone
            if (win == 0 && a[i] > 1)
                win = 1;
        }
 
        // Stone will be removed by A
        else {
 
            // If A wants to win
 
            // A will take n-1 stones from
            // current pile having n stones
            // and force B to pick 1 stone
            if (win == 1 && a[i] > 1)
                win = 0;
        }
    }
 
    // Print the winner accordingly
    if (win == 0)
        cout << "A";
    else
        cout << "B";
}
 
// Driver Code
int main()
{
    // Given piles of stone
    int arr[] = { 1, 1, 1, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    findWinner(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the winner
// of game between A and B
static void findWinner(int a[], int n)
{
     
    // win = 1 means B is winner
    // win = 0 means A is winner
    int win = 0;
 
    // If size is even, winner is B
    if (n % 2 == 0)
        win = 1;
 
    // If size is odd, winner is A
    else
        win = 0;
 
    for(int i = n - 2; i >= 0; i--)
    {
         
        // Stone will be removed by B
        if (i % 2 == 1)
        {
             
            // If B wants to win
 
            // B will take n-1 stones
            // from current pile having
            // n stones and force A to
            // pick 1 stone
            if (win == 0 && a[i] > 1)
                win = 1;
        }
 
        // Stone will be removed by A
        else
        {
             
            // If A wants to win
 
            // A will take n-1 stones from
            // current pile having n stones
            // and force B to pick 1 stone
            if (win == 1 && a[i] > 1)
                win = 0;
        }
    }
 
    // Print the winner accordingly
    if (win == 0)
        System.out.print("A");
    else
        System.out.print("B");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given piles of stone
    int arr[] = { 1, 1, 1, 2 };
 
    int N = arr.length;
 
    // Function call
    findWinner(arr, N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to find the winner
# of game between A and B
def findWinner(a, n):
     
    # win = 1 means B is winner
    # win = 0 means A is winner
    win = 0
 
    # If size is even, winner is B
    if (n % 2 == 0):
        win = 1
 
    # If size is odd, winner is A
    else:
        win = 0
 
    for i in range(n - 2, -1, -1):
 
        # Stone will be removed by B
        if (i % 2 == 1):
 
            # If B wants to win
 
            # B will take n-1 stones
            # from current pile having
            # n stones and force A to
            # pick 1 stone
            if (win == 0 and a[i] > 1):
                win = 1
 
        # Stone will be removed by A
        else:
 
            # If A wants to win
 
            # A will take n-1 stones from
            # current pile having n stones
            # and force B to pick 1 stone
            if (win == 1 and a[i] > 1):
                win = 0
                 
    # Print the winner accordingly
    if (win == 0):
        print("A")
    else:
        print("B")
 
# Driver Code
if __name__ == '__main__':
     
    # Given piles of stone
    arr = [ 1, 1, 1, 2 ]
 
    N = len(arr)
 
    # Function call
    findWinner(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG{
 
// Function to find the winner
// of game between A and B
static void findWinner(int []a,
                       int n)
{   
  // win = 1 means B is winner
  // win = 0 means A is winner
  int win = 0;
 
  // If size is even, winner is B
  if (n % 2 == 0)
    win = 1;
 
  // If size is odd, winner is A
  else
    win = 0;
 
  for(int i = n - 2; i >= 0; i--)
  {
    // Stone will be removed by B
    if (i % 2 == 1)
    {
 
      // If B wants to win
 
      // B will take n-1 stones
      // from current pile having
      // n stones and force A to
      // pick 1 stone
      if (win == 0 && a[i] > 1)
        win = 1;
    }
 
    // Stone will be removed by A
    else
    {
      // If A wants to win
      // A will take n-1 stones from
      // current pile having n stones
      // and force B to pick 1 stone
      if (win == 1 && a[i] > 1)
        win = 0;
    }
  }
 
  // Print the winner accordingly
  if (win == 0)
    Console.Write("A");
  else
    Console.Write("B");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given piles of stone
  int []arr = {1, 1, 1, 2};
 
  int N = arr.Length;
 
  // Function call
  findWinner(arr, N);
}
}
 
 // This code is contributed by Rajput-Ji


Javascript


输出:
B

时间复杂度: O(N)
辅助空间: O(1)

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