Jar of Water Game
Time limit0.5sMemory limit512 MB
Simulate a circular card-passing game with N players, a wildcard, and a fixed passing rule until one player holds four equal cards.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Greedy
- Solved
- No attempts yet
Problem
The schedule of the day for a programming contest usually follows the same pattern: warmup in the morning, followed by lunch, some rest time, final adjustments of the competition environment, and then the start of the contest.
At rest time some contestants prefer to relax, others prefer to socialize, and some have a habit of playing a card game. Luciano and his friends enjoy playing a game known as "Jar of Water". Tired of never winning, Luciano wants to write a program that, given the initial cards of all players (do not ask how he knows them), determines whether he will win. If he is not going to win, he can then make up any excuse and ask not to participate in that round.
The game works as follows:
- The deck used has the cards "A23456789DQJK" (in this order, from lowest to highest value), where suits are ignored. The deck also has one extra card: a wildcard.
- N contestants sit side by side in a circle. Contestant 1 is immediately to the left of contestant 2, who is immediately to the left of contestant 3, and so on until the circle is completed by the N-th contestant sitting immediately to the left of contestant 1. A contestant K is selected randomly to start the game.
- In a game with N contestants there are four cards of each of N different values and one wildcard. At the start of the game, contestant K receives the wildcard. The remaining cards are shuffled and dealt so that each player receives four of them.
- In each round, the contestant in turn chooses one of his cards and passes it to the contestant on his right. The contestant who receives the card is next in turn.
- A contestant is in a winning state if he has exactly four cards in hand and they all have the same value. The game ends as soon as at least one contestant is in the winning state. In that case, the contestant with the lowest number among those in the winning state is declared the winner.
The card passed from one contestant to the next is selected by the following rule:
- The wildcard can never be passed immediately after being received. This also applies to the starting player, who received the wildcard from the dealer just before the first round.
- The contestant will, whenever possible, pass the wildcard to the next player.
- If the contestant cannot pass the wildcard, the contestant chooses the card that appears least in his hand and passes that card to the next contestant. If more than one card appears least often, the contestant selects, among those, the one with the lowest value in the order described above.
Now that you know the rules, help Luciano write a program that, given the initial setup of the game, tells which player will be declared the winner.
Input
The first line of input contains two integers N and K (2 ≤ N ≤ 13, 1 ≤ K ≤ N) representing the number of contestants in the game and the contestant who starts the game, respectively. Each of the following N lines contains four characters representing the initial cards for the i-th contestant (not counting the wildcard).
Output
Your program must output a single line containing an integer, the number of the contestant who will be declared the winner of the game.