Integration of Lines and Poker
Time limit1sMemory limit2048 MB
Simulate swaps on a grid of colored pieces with special effects and gravity, then total the elimination, chain, combo, pattern, and endgame scores.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Matrix
- Solved
- No attempts yet
Problem
You are given a board of size , with the top left corner at and the bottom right corner at . There are different colors of pieces, numbered from to . Initially, each cell contains one piece.
There are operations in total. Each operation swaps two adjacent (up, down, left, or right) pieces. After the swap, every run of at least pieces of the same color in a row or column is eliminated.
After elimination, pieces fall down due to gravity, leaving empty cells at the top of each column. Once all pieces have fallen, any pieces that can now be eliminated are removed, and this continues as a chain reaction until nothing else can be eliminated. One elimination followed by one fall is called a "round of elimination". The number of rounds triggered by one operation is its "number of rounds".
Some pieces have special properties that trigger a special effect when they are eliminated. There are types of special properties:
- Eliminating the piece removes all pieces in the same row.
- Eliminating the piece removes all pieces in the same column.
- Eliminating the piece removes all pieces in the same row and column.
- Eliminating the piece removes all pieces in the square centered on it.
- Eliminating the piece removes all pieces in the square centered on it.
- Eliminating the piece removes all pieces of the same color.
A special effect can trigger other special effects. All of these happen in the same round, as a chain, before gravity applies.
An operation is valid only if the two positions are adjacent, both are non-empty, and the swap causes at least one elimination. Invalid operations are skipped. The game ends after all operations are processed.
The main color of a valid operation is the color eliminated directly by the swap, not counting eliminations caused by special effects or falling. A valid operation has or main colors.
The player scores points from five parts: elimination score, chain score, combination score, pattern score, and endgame score.
- Elimination score: In round of a valid operation, the score is times the sum of the color numbers of all pieces eliminated in that round.
- Chain score: If a valid operation has rounds in total, the chain score is .
- Combination score: In a round, consider only eliminations caused by at least consecutive pieces of the same color in a row or column. Special effects do not count here. If a connected same-color block has size , its combination score is . For example, pieces in a line score . pieces forming a line, a cross, or a T-shape score . A block of same-color pieces, which can appear after a fall, scores .
- Pattern score: After every valid operations, a pattern score is computed from the main colors of those operations. If an operation has two main colors, use whichever choice gives the higher score. The rules are:
- High card: All colors are different. Score is plus the highest color number.
- One pair: One pair of the same color and other colors. Score is plus the pair's color number times .
- Two pairs: Two pairs and other color. Score is plus the larger pair's color number times plus the smaller pair's color number.
- Three of a kind: pieces of one color and other colors. Score is plus the triple's color number times .
- Full house: pieces of one color and pieces of another. Score is plus the triple's color number times plus the pair's color number.
- Four of a kind: pieces of one color and other color. Score is plus the quadruple's color number times .
- Five of a kind: All pieces are the same color. Score is plus the color number times .
- Endgame score: If all operations are valid, the player gets a bonus of . If the board is completely cleared at the end, the player gets a bonus of .
Given the initial state, the special properties, and every operation, compute the player's total score.
Input
The first line contains four integers , , , and (; ; ; ).
The next lines each contain integers , the initial colors from top to bottom and left to right ().
The following lines each contain integers , the initial special properties (, where means no special property).
Each of the last lines contains four positive integers , , , and , meaning the pieces at and are swapped (; ).
The initial state has no direct eliminations.
Output
Print one integer, the total score at the end of the game.
Hint
The sums of the first three score types after each operation are , , , , and . After the th operation, the pattern score is computed. The best pattern is , which scores . At the end, both endgame bonuses are awarded, so the total is .