Under construction

Still being built. Things may change

Integration of Lines and Poker

Time limit1sMemory limit2048 MB

Summary
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 n×mn \times m, with the top left corner at (1,1)(1, 1) and the bottom right corner at (n,m)(n, m). There are kk different colors of pieces, numbered from 11 to kk. Initially, each cell contains one piece.

There are qq operations in total. Each operation swaps two adjacent (up, down, left, or right) pieces. After the swap, every run of at least 33 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 66 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 3×33 \times 3 square centered on it.
  • Eliminating the piece removes all pieces in the 5×55 \times 5 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 qq 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 11 or 22 main colors.

The player scores points from five parts: elimination score, chain score, combination score, pattern score, and endgame score.

  • Elimination score: In round ii of a valid operation, the score is ii times the sum of the color numbers of all pieces eliminated in that round.
  • Chain score: If a valid operation has xx rounds in total, the chain score is 80⋅(x−1)280 \cdot (x - 1)^2.
  • Combination score: In a round, consider only eliminations caused by at least 33 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 xx, its combination score is 50(x−3)250(x-3)^2. For example, 44 pieces in a line score 5050. 55 pieces forming a line, a cross, or a T-shape score 200200. A 2×32 \times 3 block of same-color pieces, which can appear after a fall, scores 450450.
  • Pattern score: After every 55 valid operations, a pattern score is computed from the main colors of those 55 operations. If an operation has two main colors, use whichever choice gives the higher score. The rules are:
    • High card: All 55 colors are different. Score is 5050 plus the highest color number.
    • One pair: One pair of the same color and 33 other colors. Score is 100100 plus the pair's color number times 22.
    • Two pairs: Two pairs and 11 other color. Score is 200200 plus the larger pair's color number times 22 plus the smaller pair's color number.
    • Three of a kind: 33 pieces of one color and 22 other colors. Score is 300300 plus the triple's color number times 33.
    • Full house: 33 pieces of one color and 22 pieces of another. Score is 500500 plus the triple's color number times 33 plus the pair's color number.
    • Four of a kind: 44 pieces of one color and 11 other color. Score is 750750 plus the quadruple's color number times 55.
    • Five of a kind: All 55 pieces are the same color. Score is 10001000 plus the color number times 1010.
  • Endgame score: If all qq operations are valid, the player gets a bonus of 10001000. If the board is completely cleared at the end, the player gets a bonus of 10 00010\,000.

Given the initial state, the special properties, and every operation, compute the player's total score.

Input

The first line contains four integers nn, mm, kk, and qq (2≤n,m≤502 \leq n, m \leq 50; m+n>4m + n > 4; 2≤k≤1002 \leq k \leq 100; 1≤q≤10001 \leq q \leq 1000).

The next nn lines each contain mm integers ai,ja_{i,j}, the initial colors from top to bottom and left to right (1≤ai,j≤k1 \leq a_{i,j} \leq k).

The following nn lines each contain mm integers bi,jb_{i,j}, the initial special properties (0≤bi,j≤60 \leq b_{i,j} \leq 6, where 00 means no special property).

Each of the last qq lines contains four positive integers x1x_1, y1y_1, x2x_2, and y2y_2, meaning the pieces at (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) are swapped (1≤x1,x2≤n1 \leq x_1, x_2 \leq n; 1≤y1,y2≤m1 \leq y_1, y_2 \leq m).

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 315315, 417417, 429429, 435435, and 482482. After the 55th operation, the pattern score is computed. The best pattern is (1 2 4 2 4)(1\ 2\ 4\ 2\ 4), which scores 200+4⋅2+2⋅1=210200 + 4 \cdot 2 + 2 \cdot 1 = 210. At the end, both endgame bonuses are awarded, so the total is 11 69211\,692.

Examples3

  1. Example 1

    Input
    8 8 5 5
    1 1 5 1 5 4 5 3
    2 1 2 2 5 4 3 2
    1 4 1 4 2 1 5 4
    2 1 5 5 2 1 4 4
    2 3 5 2 3 4 2 2
    4 2 4 3 3 2 4 5
    1 3 4 3 5 2 4 3
    3 4 2 5 2 1 1 2
    0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0
    0 0 0 0 5 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 6 0 0 3 1
    0 0 0 0 3 0 0 0
    0 0 0 0 0 0 1 4
    3 2 4 2
    5 4 5 5
    7 2 7 3
    8 5 8 6
    6 7 6 8
    
    Expected output
    11692
    
  2. Example 2

    Input
    8 8 5 8
    1 1 5 1 5 4 5 3
    2 1 2 2 5 4 3 2
    1 4 1 4 2 1 5 4
    2 1 5 5 2 1 4 4
    2 3 5 2 3 4 2 2
    4 2 4 3 3 2 4 5
    1 3 4 3 5 2 4 3
    3 4 2 5 2 1 1 2
    0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0
    0 0 0 0 5 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 6 0 0 3 1
    0 0 0 0 3 0 0 0
    0 0 0 0 0 0 0 0
    1 1 2 2
    3 2 4 2
    3 2 3 3
    4 2 4 3
    5 4 5 5
    7 2 7 3
    8 5 8 6
    6 7 6 8
    
    Expected output
    684
    
  3. Example 3

    Input
    5 5 2 1
    1 1 2 1 1
    1 1 2 1 1
    2 2 1 2 2
    1 1 2 1 1
    1 1 2 1 1
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    3 3 4 3
    
    Expected output
    3023