给定一个由N个整数组成的数组arr [] ,每个整数代表一堆石头的大小。任务是确定当两个玩家A和B根据以下条件最佳玩游戏时,确定游戏的获胜者:
- 玩家A总是开始游戏。
- 在一个步骤中,玩家可以从索引最小的第一非空堆中清除任意数量的石头(至少1个)。
- 第一个无法采取行动的玩家将输掉比赛。
如果玩家A赢得游戏,则打印“ A” 。否则,打印“ B” 。
例子:
Input: arr[] = {1, 1, 1, 1, 1, 1}
Output: B
Explanation:
Here, each pile has only one stone so A and B will alternatively remove one stone each.
A removes 1 stone from 1st pile, B removes 1 from 2nd pile and so on.
Last stone in 6th pile is removed by B.
Since A has no choice left, B wins the game.
Input: arr[] = {1, 1, 2, 1}
Output: A
Explanation:
Here,
A removes 1 stone from 1st pile,
B removes 1 from 2nd pile,
A removes only 1 from 3rd pile,
and now B is forced to remove 1 from 3rd pile.
Last stone in 4th pile is removed by A.
Since B has no choice left, A wins the game.
方法:想法是弄清楚玩家赢得比赛应遵循的最佳方式。以下是两种最佳玩法:
- 对于除最后一个堆以外的所有堆,如果堆上有K块石头,则当前玩家将仅选择( K – 1 )块石头(仅当K> 1时),以便另一名玩家被迫选择剩余的1块石头。这保证了当前玩家有机会从下一堆中挑石头并最终获胜。
- 对于最后一堆,如果它有K块石头,那么当前玩家将拾取所有K块石头,以便其他玩家没有机会拾取石头。这最终保证了当前玩家获胜。
请按照以下步骤解决此问题:
- 最初,假设玩家将从当前桩除去所有的石头,A将是赢家如果数组的大小是奇数和B如果尺寸是均匀的。
- 现在,在[0,N – 2]范围内进行迭代,并检查当前索引和当前堆中出现的石头数量,以确定哪个玩家将获胜。
- 遍历时,如果索引为偶数,则A将有机会捡石头。否则,B将有机会捡石头。
- 如果当前指数为偶数且宝石数大于1 ,则将获胜者设置为B。将获胜者更新为A。
- 如果当前指数为奇数,并且宝石数超过1 ,则将获胜者设置为A。然后,将获胜者更新为B。
- 最后,打印游戏的获胜者。
下面是上述方法的实现:
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
B
时间复杂度: O(N)
辅助空间: O(1)