Integration of Lines and Poker

시간 제한1초메모리 제한2048 MB

요약
특수 조각의 연쇄 효과가 포함된 3매치 퍼즐 보드를 q회 조작한 뒤 규직에 잘린 점수 보너스까지 더한 총점 구합니다.
난이도

어려움10점 중 10점

유형
시뮬레이션, 구현, BFS, 비트 연산
정답자
아직 제출이 없습니다

문제

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 involves swapping two adjacent (up, down, left, right) pieces. After this, every continuous sequence of at least 33 pieces of the same color in the same row or column will be eliminated.

After elimination, all pieces will fall down due to gravity, creating empty spaces at the top of the column. Once all pieces have fallen, if there are still pieces that can be eliminated, a chain reaction will occur, continuing to eliminate until no more eliminations are possible. A single elimination followed by a fall is called "one round of elimination", and we can define the "number of rounds" of elimination triggered by one operation.

Some pieces have special properties that trigger special effects when eliminated. There are a total of 66 types of special properties:

  1. Eliminating will remove all pieces in the same row;
  2. Eliminating will remove all pieces in the same column;
  3. Eliminating will remove all pieces in the same row and column;
  4. Eliminating will remove all pieces in a 3×33 \times 3 square centered on it;
  5. Eliminating will remove all pieces in a 5×55 \times 5 square centered on it;
  6. Eliminating will remove all pieces of the same color.

Triggering a piece's special effect may also trigger other pieces' special effects, but these are all triggered within the same round of elimination, as a chain reaction, before falling due to gravity.

In the game, each operation must be valid, meaning the two positions involved in the operation must be adjacent and not empty, and the operation must lead to a piece elimination. If an operation is not valid, it is skipped. The game ends after all qq operations are completed.

The main color of a valid operation is defined as the color that is directly eliminated by the swap (not including those triggered by special effects or falling). It is easy to see that a valid operation has 11 or 22 main colors.

In the game, players aim to score as many points as possible through their operations. The scoring rules consist of 55 types: elimination score + chain score + combination score + pattern score + endgame score.

\begin{itemize}

  • Elimination score: For each valid operation, the elimination score for the ii-th round of elimination is ii times the sum of the color numbers of all pieces eliminated in that round.

  • Chain score: If the total number of elimination rounds for a valid operation is xx, the chain score is 80⋅(x−1)280 \cdot (x - 1)^2.

  • Combination score: In a certain round of elimination, consider only the eliminations caused by "at least 33 consecutive pieces of the same color in the same row or column" (disregard eliminations caused by special effects). If a certain eliminated same-color block connected by side has size xx, the combination score is 50(x−3)250(x-3)^2. Some examples: 44 same-color pieces in a line give a combination score of 5050; 55 same-color pieces forming a line, cross, or T-shape give a score of 200200; a 2×32 \times 3 square of same-color pieces (which may appear after a fall) gives a score of 450450.

  • Pattern score: Every 55 valid operations, a pattern score is calculated based on the main colors of the previous 55 valid operations (if an operation has multiple main colors, take the one that can yield the maximum score according to the following rules):

    • High card: All 55 colors are different, score is 5050 + the highest color number among all cards;
    • One pair: 22 pieces of the same color + 33 pieces of different colors, score is 100100 + the pair's color number ×2\times 2;
    • Two pairs: 22 same-color pairs + 11 other color, score is 200200 + the larger color number of the pairs ×2\times 2 + the smaller color number of the pairs;
    • Three of a kind: 33 pieces of the same color + 22 different colors, score is 300300 + the three of a kind's color number ×3\times 3;
    • Full house: 33 pieces of one color + 22 pieces of another color, score is 500500 + the color number of the three of a kind ×3\times 3 + the color number of the pair;
    • Four of a kind: 44 pieces of the same color + 11 other color, score is 750750 + the four of a kind's color number ×5\times 5;
    • Five of a kind: All 55 pieces are the same color, score is 10001000 + the five of a kind's color number ×10\times 10.
  • Endgame score: If all qq operations are valid, 10001000 bonus points are awarded at the end. If the board is completely cleared at the end of the game, 10,00010\\,000 bonus points are awarded.

Given the initial state of a game and each operation performed by the player, you need to calculate the player's total score.

입력

The first line of input 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 containing mm integers a_i,ja\_{i,j}, represent the initial state of the pieces' colors from top to bottom and from left to right (1≤a_i,j≤k1 \le a\_{i,j} \leq k).

The following nn lines, each containing mm integers b_i,jb\_{i,j}, represent the initial state of the pieces' special effects, as described in the problem statement. Specifically, b_i,j=0b\_{i,j} = 0 indicates no special effect (0≤b_i,j≤60 \le b\_{i,j} \leq 6).

Each of the next qq lines contains four positive integers: x_i,1x\_{i,1}, y_i,1y\_{i,1}, x_i,2x\_{i,2}, and y_i,2y\_{i,2}. They indicate the swap of pieces at coordinates (x_i,1,y_i,1)(x\_{i,1}, y\_{i,1}) and (x_i,2,y_i,2)(x\_{i,2}, y\_{i,2}) (1≤x_i,1,x_i,2≤n1 \leq x\_{i,1}, x\_{i,2} \leq n; 1≤y_i,1,y_i,2≤m1 \le y\_{i,1}, y\_{i,2} \leq m).

It is guaranteed that the initial state has no direct elimination situations.

출력

Output a single line with an integer representing the total score at the end of the game.

힌트

The sums of the first three types of scores after each operation are: 315315, 417417, 429429, 435435, 482482. After the 55-th operation, the pattern score is calculated, with the optimal pattern being (1 2 4 2 4)(1\ 2\ 4\ 2\ 4), yielding a score of 200+4⋅2+2⋅1=210200 + 4 \cdot 2 + 2 \cdot 1 = 210. At the end of the game, we obtain both types of endgame bonuses, resulting in the total score of 11,69211\\,692.

예제3

  1. 예제 1

    입력
    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
    
    예상 출력
    11692
    
  2. 예제 2

    입력
    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
    
    예상 출력
    684
    
  3. 예제 3

    입력
    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
    
    예상 출력
    3023