给定N堆仅包含White(W)和Black(B)的盒子,则两个玩家A和B玩游戏。
玩家A可以从具有最高框White的堆的顶部移除任何数量的盒子,而玩家B可以从具有最高框Black的堆的顶部移除任何数量的盒子。如果有一个顶部为W的堆,则玩家A已移除一个或多个盒子。类似地,如果有一个顶部为B的堆,则玩家B必须删除一个或多个盒子。他们轮流玩,玩家A先移动。两名球员都发挥最佳状态。
任务是找到游戏的赢家(谁不能走最后一步)。玩家最后一步输了比赛。
例子:
Input: N = 2
WBW, BWB
Output: A
Player A can remove all boxes from pile 1. Now player B has to make a choice from pile 2. Whatever choice player B makes, he/she has to make the last move.
Input: N = 3
WWWW, WBWB, WBBW
Output: B
方法:博弈论问题,即最后采取行动的玩家输掉游戏,被称为米塞雷·尼姆(Misere Nim)的游戏。
- 如果在同一堆中连续出现同一盒子,我们可以简单地用同一盒子的一个副本替换它们,因为最好删除所有出现在顶部的盒子。因此,我们可以压缩所有桩以得到形式为BWBWBW或WBWBWB的桩。
- 现在,假设有一个堆,其最上面的箱子与最下面的箱子不同,我们可以证明即使没有这个堆,答案也保持不变,因为如果一个玩家在该堆上进行移动,另一位玩家则通过移除来进行下一移动同一堆箱子,第一位玩家的位置完全相同。
- 如果一堆的最顶部和最底部的框都相同,则需要一个或玩家额外移动一格。因此,只需计算每个玩家必须进行的额外移动的数量即可。用完多余动作的玩家将首先赢得比赛。
下面是上述方法的实现。
C++
// Program to find winner of the game
#include
using namespace std;
// Function to return find winner of game
string Winner(int n, string pile[])
{
int a = 0, b = 0;
for (int i = 0; i < n; ++i) {
int l = pile[i].length();
// Piles begins and ends with White box 'W'
if (pile[i][0] == pile[i][l - 1] && pile[i][0] == 'W')
a++;
// Piles begins and ends with Black box 'B'
if (pile[i][0] == pile[i][l - 1] && pile[i][0] == 'B')
b++;
}
if (a <= b)
return "A";
else
return "B";
}
// Driver code
int main()
{
int n = 2;
string pile[n] = { "WBW", "BWB" };
// function to print required answer
cout << Winner(n, pile);
return 0;
}
Java
// Program to find winner of the game
class GFG
{
// Function to return find winner of game
static String Winner(int n, String []pile)
{
int a = 0, b = 0;
for (int i = 0; i < n; ++i)
{
int l = pile[i].length();
// Piles begins and ends with White box 'W'
if (pile[i].charAt(0) == pile[i].charAt(l - 1) &&
pile[i].charAt(0) == 'W')
a++;
// Piles begins and ends with Black box 'B'
if (pile[i].charAt(0) == pile[i].charAt(l - 1) &&
pile[i].charAt(0) == 'B')
b++;
}
if (a <= b)
return "A";
else
return "B";
}
// Driver code
public static void main(String[] args)
{
int n = 2;
String pile[] = { "WBW", "BWB" };
// function to print required answer
System.out.println(Winner(n, pile));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 code to find winner of the game
# Function to return find winner of game
def Winner(n, pile):
a, b = 0, 0
for i in range(0, n):
l = len(pile[i])
# Piles begins and ends with White box 'W'
if (pile[i][0] == pile[i][l - 1] and
pile[i][0] == 'W'):
a += 1
# Piles begins and ends with Black box 'B'
if (pile[i][0] == pile[i][l - 1] and
pile[i][0] == 'B'):
b += 1
if a <= b:
return "A"
else:
return "B"
# Driver code
if __name__ == "__main__":
n = 2
pile = ["WBW", "BWB"]
# function to print required answer
print(Winner(n, pile))
# This code is contributed by Rituraj Jain
C#
// Program to find winner of the game
using System;
class GFG
{
// Function to return find winner of game
static String Winner(int n, String []pile)
{
int a = 0, b = 0;
for (int i = 0; i < n; ++i)
{
int l = pile[i].Length;
// Piles begins and ends with White box 'W'
if (pile[i][0] == pile[i][l - 1] &&
pile[i][0] == 'W')
a++;
// Piles begins and ends with Black box 'B'
if (pile[i][0] == pile[i][l - 1] &&
pile[i][0] == 'B')
b++;
}
if (a <= b)
return "A";
else
return "B";
}
// Driver code
public static void Main(String[] args)
{
int n = 2;
String []pile = { "WBW", "BWB" };
// function to print required answer
Console.WriteLine(Winner(n, pile));
}
}
// This code is contributed by Princi Singh
输出:
A
时间复杂度: O(N)