#P7147. Mahjong Game Simulator

    ID: 20351 Type: Default 1000ms 256MiB

Mahjong Game Simulator

Mahjong Game Simulator

This problem requires you to simulate a complete round of a four-player Mahjong game. The rules have been modified for simplicity and fun. A deck of \(148\) tiles is used, consisting of \(37\) distinct types of tiles with each type appearing exactly 4 times. The 37 types are:

  • Numerical tiles: 1M to 9M, 1P to 9P, 1S to 9S
  • Honor tiles: E, S, W, N, B, F, Z
  • Special tiles: PASS, REVERSE, DOUBLE

Four players, named A, B, C and D, are involved. The game proceeds as follows:

  • Before the game, the deck is arranged in a given order. Each player draws 13 tiles in turn in the order A, B, C, D (repeating as needed).
  • Then, starting with A, the players take turns. In each turn, a player draws the top tile from the deck and adds it to their hand, then discards one tile from their hand according to a fixed strategy.
  • The fixed discard strategy is as follows:
    • If the hand contains any special tiles, the player will discard one special tile. When multiple special tiles are available, they follow the priority order: PASS > REVERSE > DOUBLE. (Note: when discarding PASS, the player must designate the next player in turn to have their upcoming turn skipped.)
    • If no special tile is present, the player chooses to discard a non-special tile which minimizes the eventual "winning distance". In case of ties, the discard priority is fixed as follows: Z, F, B, N, W, S, E, 9S, 8S, …, 1S, 9P, …, 1P, 9M, …, 1M.
  • After a tile is discarded, other players (except the discarder) check if by adding that tile to their hand they can win (called RON). If multiple players are eligible, only the first one in turn order (starting from the discarder) wins.
  • A win can also occur immediately after a player draws a tile. This win is called SELFDRAWN.
  • A winning hand must satisfy the following conditions:
    • If a player has made \(n\) melds (from prior "chow" or "pong", which are not simulated in this problem), the hand must have exactly \(14-3n\) tiles.
    • The hand must not contain any special tiles (i.e. PASS, REVERSE, DOUBLE).
    • The tiles can be partitioned into \(4-n\) groups of 3 tiles, where each group is either a sequence (three consecutive numbers in the same suit, e.g. \(4P\,5P\,6P\)) or a triplet (three identical tiles), plus one pair (two identical tiles). In particular, when there are no melds (\(n=0\)), the hand of 14 tiles should be split into 4 groups of 3 and 1 pair.
  • Special tile effects upon discard:
    • PASS: When discarded, the player selects the next player in turn order to skip their upcoming turn.
    • REVERSE: Reverses the turn order. After playing a REVERSE, the next turn goes to the player who was previously the discarder’s predecessor in the old order.
    • DOUBLE: Grants the discarding player an extra turn immediately.
  • If the deck becomes empty before any win occurs, the game ends in a draw.

Note: For simplicity, this simulation does not include the complexities of making melds through chow or pong. All players follow the fixed strategy as described. The simulation should output the final result of the game.

Input/Output Details:

You are given a complete ordering of the 148 tiles. Simulate the game according to the rules. If a player wins by self-draw, output the player’s identifier followed by a space and then SELFDRAWN. If a player wins by ron, output the player’s identifier followed by a space and then RON. If no one wins by the time the deck is exhausted, output DRAW.

Winning Hand Check (in \(\LaTeX\)): A hand (with no melds) is winning if and only if it has \(14\) tiles (and no special tiles) and the tiles can be partitioned as \[ \underbrace{\{3\text{-tile groups}\}}_{4\text{ groups}} \cup \{\text{a pair}\} \] where each 3-tile group is either a chow (\(a, a+1, a+2\) in the same suit) or a triplet (three of the same tile), and the pair consists of two identical tiles.

inputFormat

A single line containing 148 space-separated tokens representing the deck order. The deck consists of 37 types of tiles (each appearing 4 times) in the following order: numerical tiles (e.g., 1M, 2M, …, 9M; 1P, …, 9P; 1S, …, 9S), honor tiles (E, S, W, N, B, F, Z), and special tiles (PASS, REVERSE, DOUBLE).

outputFormat

Output the result of the simulated game. If a player wins by self-draw, output the player's identifier and 'SELFDRAWN' separated by a space. If a player wins by ron (winning off another player's discard), output the player's identifier and 'RON'. If no win occurs and the deck is exhausted, output 'DRAW'.

sample

1M 1M 1M 1M 2M 2M 2M 2M 3M 3M 3M 3M 4M 4M 4M 4M 5M 5M 5M 5M 6M 6M 6M 6M 7M 7M 7M 7M 8M 8M 8M 8M 9M 9M 9M 9M 1P 1P 1P 1P 2P 2P 2P 2P 3P 3P 3P 3P 4P 4P 4P 4P 5P 5P 5P 5P 6P 6P 6P 6P 7P 7P 7P 7P 8P 8P 8P 8P 9P 9P 9P 9P 1S 1S 1S 1S 2S 2S 2S 2S 3S 3S 3S 3S 4S 4S 4S 4S 5S 5S 5S 5S 6S 6S 6S 6S 7S 7S 7S 7S 8S 8S 8S 8S 9S 9S 9S 9S E E E E S S S S W W W W N N N N B B B B F F F F Z Z Z Z PASS PASS PASS PASS REVERSE REVERSE REVERSE REVERSE DOUBLE DOUBLE DOUBLE DOUBLE
DRAW