The Game of 31
Time limit1sMemory limit128 MB
Given a partly played game of 31 with four cards of each value 1 to 6, determine the winner under perfect play.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming, Backtracking, Combinatorics
- Solved
- No attempts yet
Problem
The game of 31 was a favourite of con artists who rode the railroads in days of yore. The game is played with a deck of cards: four of each of 1, 2, 3, 4, 5, 6 (four cards labelled 1, four labelled 2, and so on). Initially all of the cards lie face up on the table and the discard pile is empty. The players then take turns. On each turn the current player picks up one unused card from the table and lays it on the discard pile. The goal is to be the last player to lay a card without letting the sum of the pile exceed . Your task is to determine the eventual winner of a partially played game, assuming both players play the remainder of the game with a perfect strategy.
For example, in the following game player wins:
- Player plays
3. - Player plays
5. - Player plays
6. - Player plays
6. - Player plays
5. - Player plays
6.
After player 's final 6 the pile totals exactly , so player can no longer move and player is the winner.
Input
The first line contains the number of test cases. Each of the following lines describes one test case: a sequence of zero or more digits representing a partially completed game. The first digit is player 's move, the second is player 's move, and so on, with the two players alternating. Finish each game using a perfect strategy for both players to determine who wins.
Output
For each game, print A or B on its own line to indicate the eventual winner.