TRACTOR - Game Simulator


Please click here to download a PDF version of the contest problems. The problem is problem G in the PDF.

Since this problem is added as an classical problem in SPOJ, the users who get this problem Accepted (by himself/herself, I'll look at your code) before 2011.10.25 8:00:00 SPOJ time (two years right after the on-site contest's end) will be e-mailed some pictures of the problem setters of this contest - g201513, lcosvse and Blue Mary. [the offer is invalid, it has been presented to Wassim Khalil (Australia).]

Log:

  1. Thanks to Thomas Schnattinger, who points out a small bug in the sample input. The corrected sample is shown below.
  2. The bug about '\r\n' has been fixed.

"Tractor" is a very popular poker game in China. There are four players in the game. Suppose their names are Alice, Bob, Charles and David, in clockwise order. A judge is needed for this game. The players are divided into two teams, Alice and Charles are in team 1, and the other two are in team 2. The prop they use to play the game are two decks of pokers, including 108 cards in total. A simplified rule of the game is described below.

The whole game contains a number of rounds. In each round, one team is called "Declarers" (CT); the other team is called "Defenders" (FT). Each team has a current rank (CR). The goal of the player is to increase his own team's CR as much as possible.

A certain round has a Main Suit (Heart - H, Spade - S, Club - C, Diamond - D, or None - no main suit in this round) and its CR. The CR in this round is the CR of the CT, and Main Suit will be given. The Main Suit and CR will be used to determine the order of the cards.

Cards ranked 5, 10, King value 5, 10, 10 pts (points) respectively, all other cards value 0 pts. In one round, we only consider the FT's pts. The rules of getting pts for FT will be discussed later.

If the FT gets less than 80 pts in one round, they will hold be FT in the next round. This situation is called "make". Otherwise, they become CT in the next round and the original CT become FT instead. This situation is called "down".

If the FT gets 0 pts, the CR of the current CT will be increased by 3, for example, if the CR of the CT is 9, it will become Queen (12). Otherwise, if the FT gets less than 40 pts, the CR of the CT will be increased by 2. Otherwise, if the FT gets less than 80 pts, the CR of the CT will be increased by 1. Otherwise, if the FT gets not less than 80 + k * 40 pts and less than 120 + k * 40 pts, the CR of the current FT will be increased by k. For example, if the FT gets 255 pts in a round, the CR of the current FT will be increased by 4; and if the FT gets 80 pts, both teams' CR remain unchanged. If a team's CR becomes beyond Ace, this team is considered the WINNER of the whole game.

During a round, one of the players in CT is called the dealer. If "make", the pard (teammate) of the dealer becomes the next round’s dealer. Otherwise ("down"), the player on dealer's right-hand side becomes the dealer of the next round. For example, if the dealer of the current round is Alice and her team (CT) is "down", the dealer of the next round should be Bob (on Alice's right-hand side).

At the start of a round, each of the players except the dealer gets exactly 25 cards; the dealer gets all the remaining 33 cards. After that, the dealer chooses 8 of his cards and gives them to the judge, and these cards are called "hidden cards".

Now each player has exactly 25 cards. A round consists of several tricks. In the first trick, the dealer plays one or more cards (called "lead"), then, in clockwise order, players play the same number of cards as the first player one by one (called "follow"). The winner of the current trick leads cards during the next trick, and so on. If the winner of the current trick is a member of FT, then the FT gets the sum of the cards' pts played in this trick.

Now we start to describe how to determine the winner of a trick. After the main suit and the CR of the current round are fixed, we can determine the "trumps" which are cards with main suit or CR (Current Rank), and the jokers. All other cards are "not-trumps".

We can have an order among all the cards according the following rules:

  1. "Trumps" are ordered higher than "not-trumps".
  2. For the trumps, the order are listed below:
    • Red Joker
    • Black Joker
    • card with main suit and CR (if exists)
    • other card with CR
    • other trumps ordered by their ranks(i.e., A, K, Q, J, T, 9, 8, 7 ... 3, 2)
  3. For the "not-trumps", they are ordered by their ranks.

Assume in all the description below, in the current round, the CR of the CT is 7.

Suppose the main suit is H, the cards can be arranged in this order (as an example):

  S2, C2, D2
< S3, C3, D3
< S4, C4, D4
< S5, C5, D5
< S6, C6, D6
< S8, C8, D8
< S9, C9, D9
< ST, FT, CT (T - 10)
< SJ, CJ, DJ (J - Jack)
< SQ, CQ, DQ (Q - Queen)
< SK, CK, DK (K - King)
< SA, CA, DA (A - Ace)
< H2
< H3
< H4
< H5
< H6
< H8
< H9
< HT
< HJ
< HQ
< HK
< HA
< S7 = C7 = D7
< H7
< BJ (the Black Joker)
< RJ (the Red Joker)

If "None" during this round, then the pokers can be arranged in this order:

  H2, S2, C2, D2
< H3, S3, C3, D3
< H4, S4, C4, D4
< H5, S5, C5, D5
< H6, S6, C6, D6
< H8, S8, C8, D8
< H9, S9, C9, D9
< HT, ST, FT, CT
< HJ, SJ, CJ, DJ
< HQ, SQ, CQ, DQ
< HK, SK, CK, DK
< HA, SA, CA, DA
< H7 = S7 = C7 = D7
< BJ
< RJ

In these two tables, cards written in italic are "trumps", and cards written in boldface are "not-trumps".

In each trick, the lead cards (played by the player leading this trick) must be either all "trumps", or all "not-trumps" with same suit.

The possible structures of the cards are listed below (assume the main suit is H and main rank is 7 for the example):

  • Single. A single card, such as D9.
  • Pair. Two same cards, such as D9D9. But D7S7 is not a pair although their orders are the same.
  • Tractor. Two or more consecutive-ordered pairs, satisfying the condition that they are all "trumps", or all "not-trumps" with same suit, such as SJSJSQSQSKSKSASA, H7H7S7S7HAHA or RJRJBJBJ. But, these are not tractors: S7S7C7C7 (their orders are the same), C7C7C6C6 (they are not consecutive-ordered), DADAD2D2 (Ace is not "one", so they are not consecutive-ordered), H2H2H4H4, or D2D2D3. Be careful: if "None" in this round, H7H7S7S7HAHA is not a tractor (H7 and S7 are same-ordered because of "None").
  • Throw. The combination of the structures above, satisfying the condition that they are all "trumps", or all "not-trumps" with same suit. Each of the Single, Pair or Tractor in a Throw is one of the Throw’s component. In the original tractor game, in some situation, the throw will be rejected. But, to keep the rule simple, we assume in this problem all the throws are accepted. For example, RJRJBJBJH7H7HQHQHJHJH9H9H6H6HAH2 contains six components: two tractors, two pairs and two single cards (RJRJBJBJH7H7-HQHQHJHJ-H9H9-H6H6-HA-H2); CACAC8C8CK contains three components: two pairs and one single card(CACA-C8C8-CK).

A throw can be treated as different list of components, for example, H2H2H3H3H4H4H5H5H6H6 can also be treated as H2H2H3H3-H4H4H5H5H6H6, or H2H2-H3H3H4H4H4H5H5-H6H6, and so on. For the lead cards, each time we choose the longest component (choose the one with the highest order to break the tie) to construct a list of components, this list is the structure of the lead cards, also the structure of the trick. So that the structure of the trick is unique.

After the first player lead his or her cards, other players follow cards one by one in clockwise order, as mentioned above.

An important part of the game is to determine the winner of a trick:

If one’s follow cards contain both "trumps" and "not-trumps", or all "not-trumps" but with different suits, this player can't be winner of this trick. Otherwise, if the lead cards are all "not-trumps" and one’s follow cards contain "not-trumps" with different suit from the lead cards, this follow player can’t be the winner of this trick.

Else, if one’s follow cards can't be constructed as the same structure of lead cards, this player can't be the winner of this trick either.

Otherwise, if the structure of this trick is not "throw", the one who played the highest-ordered card wins this trick. If more than one player played the same highest-ordered card, the winner of this trick will be the one who plays the highest-ordered card first.

Now let's consider the "throw" situation. We construct the follow cards into the structure of the lead cards, so that the order of the highest-ordered card in all the longest components of the "throw" is as high as possible (this card is called "honor card"). Note that tractor can be treated as several pairs or shorter tractors, and pair can be treated as two single cards. The winner of this trick is the one who plays the highest-ordered "honor card". Similarly, if more than one player played the same highest-ordered "honor card" the winner of this trick will be the one who plays the highest-ordered "honor card" first.

There are many hair-raising rules about lead and follow cards; fortunately, they're not related to this problem, the only thing we care about is: when someone leads a "not-trump" "throw" the only possible way to beat it is to "throw" the same structure of "trumps". And it’s impossible to beat a leading "trump" "throw".

Special attention on the examples below. In these examples, Alice always leads cards. And assume in all the following examples, the CR is 7, and the main suit is H.

AliceBobCharlesDavidWinnerComments
SAS2STS5AliceAlice plays the highest-ranked card SA.
SAS2STSAAliceAlice plays the first SA.
SAS2STH2DavidDavid plays the first only "trump".
SAH2C7D7CharlesCharles plays "trump" C7, while David plays D7 with the same order of C7.
C2C2C3C4C7D7RJBJAliceThe structure of this trick is "pair", while all players except Alice play two single cards.
D3D3DTDTSKSKH2H3BobBob plays a pair with order higher than Alice's, while Charles discard a pair with wrong suit.
D3D3DTDTSKSKH2H2DavidDavid plays the only "trump" pair.
D6D6D8D8DJDJDKDKDTDTD2D3HTHTBJBJAliceAlice plays the only tractor (because the CR is 7).
H6H6H8H8H7H7BJBJC2C2C3C4HKHKRJRJBobBob's tractor is higher-ordered than Alices'.
H6H6H8H8H7H7D7D7C2C2C3C4HKHKRJRJBobBob also plays a tractor!
SASKSTSTC2H3S7SKAliceAlice makes a throw.
SASKHKH3HAH2S7SKCharlesBoth Bob and Charles can beat Alice.
SASKHAH2HAH3S7SKBobBoth Bob and Charles can beat Alice, but Bob's HA comes first.
S2S2S3S3SAH3H3H4H4RJD7D7H7H7H2S7S7SQSJS6CharlesBoth Bob and Charles can beat Alice.

There is a special rule about "hidden cards": if the winner of the last trick of a certain round is a member of FT, then, in addition, the FT gets the sum of the hidden cards' pts, multiplied by 2w (2 to the power of w). When the structure of the final trick is not "throw", then w is the number of lead cards of the last trick of this round. If the structure is "throw" instead, w is the length of the longest components, in the example RJRJBJBJH7H7HQHQHJHJH6H6HA, the w is 6 because the length of RJRJBJBJH7H7 (the longest components of the "throw") is 6.

To make the problem easier, you are only to write a single-round tractor game simulator.

Input

Multiple test cases, the number of them T is given in the very first line.

For each test case:

The first line contains the main suit of this round (H, S, C, D, O; O denotes "None" in this round), the dealer of this round, the CR of team 1, the CR of team 2, separated by single spaces. Each of the rest lines contains 4 strings: the lead cards and the cards played by the second, third and last player. In one string, the cards can be given in any order. Each player will play exactly 25 cards in one round.

You may assume the input is always valid.

There is a blank line between consecutive test cases, and a blank line also appears between T and the first test case.

Output

For each test case:

The first line contains the case number.

The second line contains the pts get by the FT in this round.

If a team wins the whole game after this round, output "Winner: Team X"(without quotes, X should be either 1 or 2) in the second line. If no team wins, output the new CR of team 1, the new CR of team 2 after this round, followed by the name of the dealer of the next round, separated by single spaces.

See the example for further details.

Example

Input:
1

O Charles 2 2
S6S6S7S7 SASKSJST STS8S4S4 S3S5SJSQ
S9S9 H3D3 C3DT SAD3
DA DQ DK D4
SKS8S5S3 RJC2D2H2 C6C8CJD9 H3CKDTD5
H7H7 H6H4 HJHQ H9H9
DJDJ DKH5 D5D4 D6D6
D8D8 C4C3 HTH5 D9D7
C5C5 C6CT H8HQ C7C4
H8 C7 HA HA
H2 RJ BJ CK
DA BJ C8 HK
S2S2C2 CQCAD2 HTHJHK C9CQCA

Output:
Case #1:
50
3 2 Alice

hide comments
Oleg: 2023-08-06 20:19:37

Brutal...

One thing that that is not so obvious from the description is that CR for input and output should be represented as a card (not as number) , i.e. T for 10.

Simes: 2023-02-04 16:36:12

I've extracted the problem from the PDF. I hope that's ok? If there are any mistakes or discrepancies, assume the PDF is correct.

Zorc: 2011-12-22 07:11:45

HAHAHAHAAHAAAAAAAAAAAAAA, FINALLYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY,
Oh My God !!!
After 56 Submissions !!!
Finally Accepted, Now I Can Live Happily Ever After =)

Re: Congratulations!

Last edit: 2010-10-14 08:28:01

Added by:Fudan University Problem Setters
Date:2009-11-01
Time limit:1s
Source limit:15000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 C99 GOSU PERL6
Resource:ACM/ICPC Regional Contest, Shanghai 2009