Welcome to ZOJ
122 - ZOJ Monthly, November 2012 - E
Japanese Mahjong II

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjong.

Japanese mahjong is usually played with 136 tiles, which can be organized into several categories:

  • Suited tiles. All suited tiles are of a rank and a suit.There are three suits of tiles, with ranks ranging from one to nine. There are four tiles of each rank and suit combination, thus there are 36 tiles in a suit, and 108 suited tiles in total.
    • The circle suit
    • The bamboo suit
    • The character suit
  • Honor tiles. Honor Tiles are tiles that do not have a rank or suit. They are divided into two categories. There are four types of Wind tiles and three types of Dragon tiles, with four of each type of honor tile. Thus, there are 16 wind tiles and 12 Dragon tiles for 28 honor tiles.
    • Wind tiles. The Wind tiles consist of four kinds of tile: East, South, West, and North.
    • Dragon tiles. The Dragon titles consist of three types of tile: Red, Green, White.

A winning hand consists of fourteen tiles, which is made of four melds (a specific pattern of three pieces) and the eyes (a pair of two identical pieces). The definition of melds and eyes is given as followed:

  • Melds are listed as followed:
    • Pong is a set of three identical tiles. You can form a pong with any tile. The tiles must be identical (you cannot mix suits). For example:
    • Kong is a set of four identical tiles, which is similar to Pong. For example:
    • Chow is a meld of three suited tiles in sequence. The meld must be in absolute numerical sequence. There is no skipping of numbers, nor does 9 loop around to 1. The sequence must be in the same suit. Honours cannot be used to make chows. For example:
  • Eyes, also known as a pair, are two identical tiles which are a component to the standard hand. For example:

Traditionally, the Rank 1 and Rank 9 tiles of all three suits are called oldman tiles, and both oldman tiles and honor tiles are called yaochu tiles.

In each game, there is a character of the game named prevailing wind, which is one among east, south, west, and north. There is another character of the game named seat wind for each player, which is one among east, south, west, and north. The player whose seat wind is east is the dealer of that game, while the others are the non-dealer. Both prevailing wind and seat wind can match to one wind tile which are used for winning and higher points.

When a hand is one tile short of winning, the hand is said to be a ready hand, or more figuratively, "on the pot". The player holding a ready hand is said to be waiting for certain tiles.

If at any point in the game a player can use another player's discard or pick up a tile from the wall to complete a winning hand, he yell out Mahjong, take the piece and reveal their hand. If the tile is another player's discard, it's called ron. If the tile is picked up from the wall, it's called tsumo. This ends the hand, and scoring commences.

There are two criteria in determining the winning points: han and fu. The steps of winning points calculation is as followed:

  1. Counting han.
  2. Counting fu.
  3. Calculating basic points based on the han and fu.
  4. Calculating winning points, by multiplying the basic points depending on whether the winner is the dealer or non-dealer, and whether the hand is won by tsumo or ron.
More details about rules of calculation come as followed.

The total number of han of all the kinds of yaku in the hand is summed up. x han yaku means x han should be added to the sum han.

  • 1 han yaku:
    • menzenchintsumo: won by tsumo.
    • pinfu: the fu only includes base fu.
    • i-pe-ko-: there are two exactly same chows in four melds.
    • tan'yaochu-: no yaochu tiles.
    • yakupai: one pong of prevailing wind, seat wind, or dragon tiles. If prevailing wind is the same as seat wind, you should count them both, so it's 2 han totally.
    • dora: one dora tile add one han.
  • 2 han yaku:
    • chi-toitsu: seven different pairs. You can't treat one kong as two different pairs.
    • chantaiyao: being neither a honro-to- nor a junchantaiyao, each melds and each eyes (chi-toitsu has seven eyes) include at least one yaochu tile.
    • ikkitsu-kan: there are 3 chows which are 123, 456, 789 in the same suit.
    • sanshokudo-jun: there are 3 chows which have the same ranks and 3 different suits.
    • sanshokudo-ko-: there are 3 pongs which have the same ranks and 3 different suits.
    • toitoiho-: there are 4 pongs.
    • honro-to-: being not a chinro-to-, all 14 tiles are yaochu tiles.
  • 3 han yaku:
    • ryanpe-ko-: there are two independent i-pe-ko-. If ryanpe-ko- were counted, the two i-pe-ko- shouldn't be counted again.
    • hon'i-so-: the hand consists of the tiles in only one suit and honor tiles.
    • junchantaiyao: being not a chinro-to-, each melds and each eyes include at least one oldman tile.
  • 6 han yaku:
    • chin'i-so-: all tiles are suited tiles in only one suit.
  • 13 han yaku:
    • kokushimuso-: 13 tiles consist of exact one of each yaochu tile plus 1 tile of any yaochu tile.
    • chu-renpo-to-: 13 tiles ranked 1,1,1,2,3,4,5,6,7,8,9,9,9 in the same suit, plus 1 tile in the same suit before with any rank.
    • chinro-to-: all 14 tiles are oldman tiles.
    • ryu-i-so-: all 14 tiles are one of following tiles: bamboo suit: 2, 3, 4, 6, 8; dragon tiles: green.

Fu is counted in the order below and then rounded up to the tens (rounding up means ceiling).

  1. A winning hand is automatically awarded 20 fu, which is called base fu.
  2. The base fu is 30 fu if one wins by ron.
  3. Add fu of the melds and the pair. (See the list below.)
    non-yaochu tiles yaochu tiles
    eyes 2 fu for seat wind tiles, prevailing wind tiles or dragon tiles. 4 fu when the seat wind and prevailing wind match. 0 fu for other tiles.
    chows 0 fu
    pongs 4 fu 8 fu
  4. Add fu according to how the waiting was. (See the list below.)
    waiting situations fu
    sequential tile waits for both sides 0 fu
    sequential single tile waits for a middle tile 2 fu
    sequential single tile waits for a right or left side (number 3 or 7) 2 fu
    single tile waits for the eyes 2 fu
    waits for either of melds of same three tiles 0 fu
  5. Add 2 fu if one wins by tsumo. However, if the winning hand is only base fu, this 2 fu are not awarded and the hand is counted as a total of 20 fu.
  6. Winning with yaku which include seven pairs (chi-toitsu) is counted as 25 fu altogether. The value is not rounded up to the tens.

The basic points of a hand is calculated as follows:

han basic points
han ≤ 4 max(fu × 2(2+han), 2000)
han = 5 2000
6 ≤ han ≤ 7 3000
8 ≤ han ≤ 10 4000
11 ≤ han ≤ 12 6000
han ≥ 13 8000
The winning points is calculated as follows:
  • When a non-dealer goes out by self-draw, the dealer pays the winner 2 × basic points, and the other two non-dealers pay the winner 1 × basic points.
  • When a non-dealer goes out by discard, the discarding player pays the winner 4 × basic points.
  • When the dealer goes out by self-drawn, all the three non-dealers pay the winner 2 × basic points.
  • When the dealer goes out by discard, the discarding non-dealer pays the winner 6 × basic points.
The actual points given by each player are rounded up to the nearest 100 (rounding up means ceiling). If there is more than one way to arrange the winning hand, the arrangement with the highest basic points is used.

Now, given a winning hand, can you answer how many winning points the winner can get?


There are multiple cases. Each case consists of following six parts in one line:

  1. One character in {'E', 'S', 'W', 'N'}, describing the prevailing wind. The meaning of each character in the set is: 'E' for East, 'S' for South, 'W' for West, and 'N' for North.
  2. One character in {'E', 'S', 'W', 'N'}, describing the winner's seat wind. the character's meaning is the same as part 1.
  3. Two characters, describing the dora.
  4. 26 characters, describing your hand consisting of 13 tiles.
  5. Two characters, describing winning tile.
  6. One integer, either 0 or 1. 0 means ron, while 1 means tsumo.
Each two adjacent parts are devided by exact one space character('\32'). There is no extra whitespace character before the first part or after the last part.

The manner for describing each type of tile is:

  • Two characters stand for one tile.
  • For Suited tiles, the first is an integer ranged from 1 to 9, the tile's rank, and the second is a character: 'p' for the circle suit, 's' for the bamboo suit, and 'm' for the character suit.
  • For Honor tiles, the first is an integer: from 1 to 7 stands for East, South, West, North, White, Green, and Red respectively. The second one is always 'z'.
We promise that the hand and the winning tile in part 4 and part 5 is legal, which means there isn't any type of tile which is not described above and there are at most four tiles for each type of tile.


For each case, output an integer, the winning points, in one line.

Sample Input

E S 1p 1z1z1z2p2p3p4p6m6m7m7m8m8m 2p 1
S W 4p 2p3p4p4p5p7s7s8p8p8p4s4s4s 3p 0
E N 5m 1p2p3p4p5p7p8p9p9s9s9s5m5m 3p 1

Sample Output



In this problem, actually in mathematics and computer science, rounding up means the mathematical operation of rounding a number up to the next higher integer. For example, rounding up to tens means 34 rounded up to 40, 30 rounded up tp 30.


Author: YU, Xiaoyao
Submit    Status