QOJ.ac

QOJ

Límite de tiempo: 60 s Límite de memoria: 2048 MB Puntuación total: 100

#5227. 瓷砖转置

Estadísticas

Bejeweled™ 是一款经典的益智游戏,玩家通过交换相邻的两个方块,试图在二维网格中凑齐三个连成一线的方块。

Isblinged™ 是 Hacker Cup 的衍生版本,在一个 $R$ 行 $C$ 列的方块网格上进行。位置 $(i, j)$ 处的方块类型为整数 $G_{i,j}$。一个“组”是指三个或更多相同类型的方块,通过左、右、上、下四个正交方向直接或间接相连。初始时,给定的网格 $G$ 可能已经包含了一些组。

玩家可以交换两个类型不同的正交相邻方块。如果交换导致其中任意一个方块处于一个包含三个或更多方块的组中,那么所有新形成的组中的方块都将被消除。

对于下方的示例(样例 2),交换第 1 行的前两个方块将消除 9 个方块:

另一方面,交换第 2 列的前两个方块将消除 12 个方块:

注意,类型为 3 的组没有被消除,因为它不包含被交换的方块。

请计算所有可能的理论交换(即有序对 $G_{i_1,j_1}$ 和 $G_{i_2,j_2}$,满足 $|i_1-i_2|+|j_1-j_2|=1$ 且 $G_{i_1,j_1} \ne G_{i_2,j_2}$)所能消除的方块总数。

数据范围

  • $1 \le T \le 105$
  • $1 \le R, C \le 3000$
  • $1 \le G_{i,j} \le 10^9$
  • 所有测试用例的 $R*C$ 之和不超过 $40{,}000{,}000$。

输入格式

输入的第一行包含一个整数 $T$,表示测试用例的数量。对于每个测试用例,首先是一行包含两个空格分隔的整数 $R$ 和 $C$。接下来有 $R$ 行,第 $i$ 行包含 $C$ 个空格分隔的整数 $G_{i,1..C}$。

输出格式

对于第 $i$ 个测试用例,打印一行,内容为 "Case #i:" 后跟一个整数,表示单次交换所能消除的方块总数。

样例

样例输入

3
2 3
1 1 2
1 2 3
3 5
1 2 1 1 1
1 1 2 2 1
1 3 3 3 1
4 4
1 1 1 4
1 2 1 1
3 2 3 4
3 2 2 2

样例输出

Case #1: 12
Case #2: 112
Case #3: 74

说明

在第一个样例中:

  • $G_{1,1}$、$G_{1,3}$ 和 $G_{2,3}$ 与任何方块交换都不会导致任何方块被消除。
  • $G_{1,2}$ 与下方的类型 2 方块交换可消除 3 个方块。
  • $G_{2,1}$ 与下方的类型 2 方块交换可消除 3 个方块。
  • $G_{2,2}$ 与上方或左侧的类型 1 方块交换,每次均可消除 3 个方块。

因此答案为 $3+3+2*3+12$。

在第二个样例中,消除非零数量方块的无序交换对为:

  • $G_{1,1} \leftrightarrow G_{1,2}$:消除 9 个方块
  • $G_{1,2} \leftrightarrow G_{1,3}$:消除 $5+3=8$ 个方块
  • $G_{1,2} \leftrightarrow G_{2,1}$:消除 $9+3=12$ 个方块
  • $G_{1,3} \leftrightarrow G_{2,3}$:消除 5 个方块
  • $G_{1,4} \leftrightarrow G_{2,4}$:消除 4 个方块
  • $G_{2,2} \leftrightarrow G_{2,3}$:消除 6 个方块
  • $G_{2,2} \leftrightarrow G_{3,2}$:消除 4 个方块
  • $G_{2,4} \leftrightarrow G_{2,5}$:消除 4 个方块
  • $G_{3,1} \leftrightarrow G_{3,2}$:消除 4 个方块

将总和乘以 2 以计算有序对,得到 $2*(9+8+12+5+4+6+4+4+4) = 112$。

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.