QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#2894. Mahjong Simulator

统计

Mahjong is a casual four-player game. Your task is to write a simulator to simulate the progress of a game.

The following describes the game rules and the decision-making process for each player in detail. Note: For the sake of implementation convenience and to make the game more interesting, the rules described here differ from mainstream Mahjong rules.

Basic Rules

A deck of Mahjong consists of 148 tiles, containing 37 types of tiles, with 4 of each type.

These 37 types are: 1M~9M (Characters), 1P~9P (Dots), 1S~9S (Bamboo), E (East), S (South), W (West), N (North), B (White), F (Green), Z (Red), and 3 special tiles: PASS, REVERSE, and DOUBLE.

There are 4 players in the game, denoted as A, B, C, and D.

Before the game starts, the 148 tiles are randomly shuffled and placed in a row, called the wall. Players always draw the front-most tile from the wall.

Starting from A, in the order ABCDABCD..., each player draws one tile from the wall until everyone has 13 tiles. These tiles form each player's hand.

Starting from A again, in the order ABCDABCD..., each player takes their turn: In a turn, a player first draws a tile into their hand, then discards one tile from their hand. This continues until someone wins or there are no more tiles to draw, at which point the game ends.

Special Tiles

  • PASS: When discarded, you can designate a player to skip their next turn.
  • REVERSE: When discarded, the order of turns is reversed, i.e., from ABCDABCD... to ADCBADCB... or from ADCBADCB... to ABCDABCD.... After discarding, the turn proceeds in the reversed order, starting from the player who was previously the discarder's predecessor.
  • DOUBLE: When discarded, the player immediately takes an extra turn.

Tile Sets

There are 3 types of sets: Sequence (Chow): 3 consecutive numerical tiles of the same suit (e.g., 4P 5P 6P). Triplet (Pong): 3 identical non-special tiles (e.g., B B B). *Pair: 2 identical non-special tiles (e.g., 9M 9M).

Chow and Pong

When a player discards a non-special tile, other players can Chow or Pong:

  • Chow: When the discarded tile can form a sequence with two tiles in your hand, you can take those two tiles and place them alongside the discarded tile. Note: Only the player whose turn would be next in the current order can Chow.
  • Pong: When the discarded tile can form a triplet with two tiles in your hand, you can take those two tiles and place them alongside the discarded tile. Pong does not have the restriction that Chow has; any player can Pong. If a player can both Chow and Pong, Pong takes priority. Chow (or Pong) is not mandatory; if the conditions are met, a player may choose not to Chow (or Pong).

Chow and Pong are collectively called "melds". For convenience, melds are not considered part of the hand.

After any player Chows (or Pongs), the turns of all players between the previous discarder and this player are skipped, and a new turn starts from the current player. However, this player skips drawing a tile in this turn and proceeds directly to discarding. In the next turn (if no Chow or Pong occurs), the order returns to normal.

Note: In these rules, there is no "Kong".

Winning Rules

A player's hand is considered a winning hand if and only if: The number of tiles is $14 - 3n$, where $n$ is the number of melds (Chow/Pong) the player has. There are no special tiles in the hand. * The tiles can be divided into $(5 - n)$ groups, where $(4 - n)$ groups consist of 3 tiles each (all sequences or triplets), and the remaining group consists of 2 tiles (a pair).

Note: This rule does not support special winning conditions like Seven Pairs, Thirteen Orphans, or Thirteen Terminals.

Additionally, the "winning distance" of a hand containing $13 - 3n$ tiles is defined as the minimum $x$ such that adding $x$ specific tiles to the hand and then removing $x - 1$ tiles results in a winning hand, with no more than 4 of any single tile type. The "winning distance" of a hand containing $14 - 3n$ tiles is defined as the minimum $x$ such that adding $x$ specific tiles to the hand and then removing $x$ tiles results in a winning hand, with no more than 4 of any single tile type.

Specifically, a winning hand has a winning distance of 0; a hand with a winning distance of 1 is called "Tenpai".

Note the "no more than 4 of any single tile type" restriction: If a hand is 1M 1M 1M 1M and the number of melds is 3, adding a 1M would make it a winning hand, but since there would be 5 1M tiles, this is not allowed, so its winning distance is not 1. However, if a hand is 1M and the number of melds is 4, but a 1M 1M 1M Pong was previously performed, it is still considered to have a winning distance of 1 (even though the missing 1M can never be obtained).

End of Game

  • Ron: After a player discards a tile, if another player's hand plus that tile forms a winning hand, that player wins by Ron. Ron takes priority over Chow and Pong.
  • If multiple players reach the Ron condition simultaneously, only the first player in the turn order, starting from the player who discarded the tile, can win by Ron. Others cannot, which is called "intercepting".
  • Self-Drawn: If a player draws a tile and their hand becomes a winning hand, they win by Self-Drawn.
  • Once a player wins by Ron or Self-Drawn, the game ends immediately, and that player wins.
  • If a player draws a tile and finds the wall is empty, the game ends immediately, which is called a "Draw".

Discarding Strategy

Each player's discarding strategy is identical and fixed: When discarding, if there are special tiles in hand, they must be discarded with priority. If there are multiple, follow the priority: PASS, REVERSE, DOUBLE. A discarded PASS must designate the next player. If there are no special tiles, calculate the winning distance for every possible discard, and choose the one that results in the minimum winning distance. If there is a tie, discard according to the priority: Z, F, B, N, W, S, E, 9S, 8S, ..., 1S, 9P, ..., 1P, 9M, ..., 1M. If a player can both Chow and Pong, prioritize Pong. Since there are only 4 of each tile, no two players can Pong simultaneously. Only Chow (or Pong) if it strictly reduces the winning distance. If multiple options reduce the winning distance, prioritize the one with larger numbers. If a player can win by Ron, they will always do so (unless intercepted). If they can win by Self-Drawn, they will always do so. They will never refuse a win.

Input

Read from standard input. The input consists of 148 lines, representing the tiles in the order they appear in the wall. Each line contains a string representing a tile. 1M, 2M, ..., 9M represent Characters; 1P, 2P, ..., 9P represent Dots; 1S, 2S, ..., 9S represent Bamboo; E, S, W, N, B, F, Z represent East, South, West, North, White, Green, Red; PASS represents skip; REVERSE represents reverse; DOUBLE represents double turn.

Output

Output to standard output. Follow these rules: When any player draws a tile (including the initial draws), output one line: x IN y where x is the player's name and y is the drawn tile. When any player discards a tile, if the tile is not PASS, output one line: x OUT y where x is the player's name and y is the discarded tile. If the discarded tile is PASS, output one line: x OUT PASS z where z is the player designated by PASS. When any player Chows, output one line: x CHOW y1 y2 y3 where x is the player's name, and y1, y2, y3 are the 3 tiles involved in the Chow, output in ascending numerical order. When any player Pongs, output one line: x PONG y1 y2 y3 where x is the player's name, and y1, y2, y3 are the 3 tiles involved in the Pong (they must be identical). When any player wins by Ron, output one line: x RON where x is the player's name. When any player wins by Self-Drawn, output one line: x SELFDRAWN where x is the player's name. * At the end of the game, if a player wins, output one line: x WIN where x is the player's name. If the game ends in a Draw, output one line: DRAW

Note: All English letters in the input and output must be uppercase.

Examples

Input 1

(See 1.in in the problem directory)

Output 1

(See 1.ans in the problem directory)

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.