QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 1024 MB Puntuación total: 100

#10055. 带卡牌的棋盘游戏

Estadísticas

你有 $H \cdot W$ 张牌,其中 $H \cdot W$ 为偶数。每张牌的正面写有一个 $1$ 到 $H \cdot W/2$ 之间的数字,且每个数字恰好出现在两张牌上。你考虑了可以用这些牌玩的纸牌游戏类型,并决定玩以下棋盘游戏。

你首先将 $H \cdot W$ 张牌正面朝下排列在一个 $H \times W$ 的矩形场地上。你的目标是通过重复回合来移除场地上所有的牌。在每一回合中,你必须翻开恰好两张牌。如果两张翻开的牌正面数字相同,你将这两张牌从场地上移除。如果不同,你将这两张牌再次翻回正面朝下。你拥有完美的记忆力,因此你记得你翻过的每一张牌的位置和数字。因此,在每一回合中,你的行动如下:

  1. 如果你已经知道两张数字相同的牌的位置,翻开这样的一对牌并将它们从场地上移除。
  2. 如果不是,翻开一张你尚未翻过且优先级最高的牌。我们稍后定义牌的优先级。
  3. 如果你已经见过这张翻开的牌的数字出现在另一张牌上,翻开那张牌并将这两张牌从场地上移除。
  4. 如果不是,翻开另一张你尚未翻过且优先级最高的牌。
  5. 如果你本回合翻开的第一张和第二张牌恰好数字相同,将这两张牌从场地上移除。
  6. 如果不是,将这两张翻开的牌正面朝下放回,为下一回合做准备。

我们将行从上到下编号为 $1$ 到 $H$。在剩余的牌中,位于最顶行的牌优先级最高。如果最顶行有多张牌,若该行初始为奇数行,则最左侧的牌优先级最高。若该行初始为偶数行,则最右侧的牌优先级最高。

在你玩完游戏后,你发现你忘记计算移除场地上所有牌所用的回合数。幸运的是,你记得牌的初始摆放位置。因此,你决定编写一个程序来计算给定初始摆放情况下移除所有牌所用的回合数。

输入格式

第一行包含两个整数 $H$ ($1 \le H \le 100$) 和 $W$ ($1 \le W \le 100$)。你可以假设 $H \times W$ 为偶数。

接下来的 $H$ 行,每行包含 $W$ 个整数。第 $i$ 行的第 $j$ 个整数表示从上往下第 $i$ 行、从左往右第 $j$ 列的牌面数字。你可以假设这 $H$ 行中的所有整数都在 $1$ 到 $H \cdot W/2$ 之间,且每个整数恰好出现两次。

输出格式

输出一个整数,表示移除所有牌所用的回合数。

样例

样例输入 1

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

样例输出 1

9

样例输入 2

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

样例输出 2

6

样例输入 3

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

样例输出 3

7

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.