#875. MS and the Food Store Ft. Mahjong
MS and the Food Store Ft. Mahjong
Background
MS is a very NB teammate of Legendary Grandmaster DiDiDi. There are endless stories about how they won the championship in various Grand Prix.
One day after they get the championship in GP of Zhijiang, MS goes to Zhijiang food street to find something to eat tonight.
After visiting every food shop in the street, he found a ”Jansou” owned by fuko_ibuki in the corner, where one could play mahjong.
There are different tiles in Majong, which can be divided into four suits: Manzu , Souzu , Pinzu , and Jihai . Manzu, Souzu, and Pinzu each have 9 kinds of tiles, and Jihai only has 7 kinds of tiles.
Here are the pictures of tiles, each row represents a suit - Manzu, Souzu, Pinzu, and Jihai.
To simplify the expression, we use **number ** + suit to represent a tile. For example, ”1m” stands for Wanzu of 1, ”4s” stands for Souzu of 4, ”7p” stands for Pinzu of 7, and Jihai are numbered in the order of the picture above, such as ”4z” for North Wind.
Mahjong starts with what we call a ”hand” of tiles, and with each turn, the player draws a new tile and discards one.
A complete hand has tiles, consisting of seven different Toitsu or four Mentsu plus a Toitsu, where Toitsu refers to two tiles of the same suit with the same number, and Mentsu includes Shuntsu and Koutsu. Shuntsuis three consecutive cards of the same suit (not including Jihai, and cannot be cycled), and Koutsuis three cards of the same suit with the same number.
For example, is Shuntsu (abbreviated as ), and 8m9m1m are not Shuntsu, is Koutsu, and is also Koutsu. And makes a complete hand.
We call a hand Tenpai , that is, it has tiles, and a complete hand can be achieved by drawing one tile.
For example, can achieve complete hand by drawing or (or awaiting and in short). So this hand is Tenpai.
In the process of learning mahjong, MS learns the concept of X-Shanten , in which ”X”represents the minimum number of rounds for a player to draw and discard tiles to achieve Tenpai. Since there are two ways to achieve the complete hand, we choose the one that is closer to the two types.
For example, can draw and discard to become , and draw and discard to become , which is Tenpai to awaiting and , so this hand () is -Shanten.
Since pairs can also achieve the complete hand, the maximum number of ”x” is (because we can change tiles to pairs to achieve the Tenpai), otherwise, is ”8-Shanten”.
we define Taatsu as tiles awaiting one to achieve a Mentsu, such as and and each tile could only be contained in one Taatsu. A simple way to calculate the ”x-Shanten” is - (Mentsu * + Taatsu), but be careful in the case when you have or more Taatsu.
fuko_ibuki gives MS n different hands, and MS needs to tell her the hand is X-Shanten. To simplify the input, we use xxxx xxxx xxxx xxxx to give a hand (or leave out if there is no tile of the suit), like would be represented as .
Input
The first line contains an integer , denoting the number of test cases. For each test case, the only line contains a ”hand” described above.
Output
For each test case, print one integer n in one line, denoting the x of ”x-Shanten”.
Samples
2
24577s13567p113z
33446m88s5599p12z
2
1