Given **N** piles containing White(W) and Black(B) boxes only, two players A and B play a game.

Player A may remove any number of boxes from the top of the pile having the topmost box White and Player B may remove any number of boxes from the top of the pile having the topmost box Black. If there is a pile with top as W, then player A has remove one or more boxes. Similarly if there is a pile with top as B, then player B has to remove one or more boxes. They play alternating, with the **player A** first to move. Both players plays optimally.

The task is to find the winner of the game (who cannot make the last move). **Player last to move loses the game**.

**Examples:**

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

**Approach:** The game theory problems, where the player who makes the last move lose the game, are called Misere Nim’s Game.

- If there are consecutive occurrences of the same box in any pile, we can simply replace them with one copy of the same box, since it is optimal to remove all occurrences of the box present at the top. So, we can compress all piles to get the pile of form BWBWBW or WBWBWB.
- Now, suppose there’s a pile, which have topmost box differing from its bottom-most box, we can prove that answer remains same even without this pile, because if one player makes a move on this pile, other player makes the next move by removing box from the same pile, resulting in exactly same position for the first player.
- If a pile has both top and bottom-most box as same, It requires one or the player to make one extra move. So, just count the number of extra move each player has to make. The player which runs of out extra moves first wins the game.

**Below is the implementation of the above approach.**

## C++

`// Program to find winner of the game ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

A

**Time Complexity: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the Game
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Determine winner of the Game by arranging balls in a row
- Determine the winner of a game of deleting Characters from a String
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Find the player who will win the Coin game
- Find the player who wins the game by removing the last of given N cards

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.