给定N堆仅包含白 (W) 和黑 (B) 框,两个玩家 A 和 B 玩游戏。
玩家 A 可以从最上面有白色框的堆的顶部取出任意数量的框,玩家 B 可以从最上面有黑色框的堆的顶部取出任意数量的框。如果有一堆顶部为 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
Javascript
输出:
A
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。