QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 1024 MB 満点: 100

#3037. 6789

統計

Jaehyun 喜欢数字。在 10 个数字中,6、7、8 和 9 是他最喜欢的。因此,他制作了一套特殊的卡片,仅包含 6、7、8 和 9。

目前,Jaehyun 有 $N \times M$ 张卡片。Jaehyun 想要制作一个神奇的 $N \times M$ 卡片矩阵。矩阵的每一行应包含 $M$ 张卡片。他已经将卡片排列成了 $N \times M$ 的矩阵形状。

图 1. 初始状态,非中心对称。

要成为神奇矩阵,该矩阵必须是中心对称的:将矩阵旋转 180 度后,结果应与原矩阵相同。例如,8 本身是中心对称的,而 6 和 9 互为中心对称。Jaehyun 不想改变卡片的位置,所以他的目标是通过仅旋转原始位置上的卡片来使矩阵达到中心对称。

图 2. 旋转两张卡片后,它们变得中心对称。

求出为了使矩阵成为神奇矩阵,你必须旋转的最少卡片数量。

输入格式

第一行包含两个整数 $N$ 和 $M$ ($1 \le N, M \le 500$)。

接下来的 $N$ 行,每行包含一个长度为 $M$ 的字符串,表示每张卡片上写的数字。保证每个字符都是 ‘6’、‘7’、‘8’ 或 ‘9’ 中的一个。

输出格式

在第一行输出为了使矩阵成为神奇矩阵,你必须旋转的最少卡片数量。如果无法使矩阵成为神奇矩阵,则输出 “-1”(不含引号)。

样例

样例输入 1

2 3
676
679

样例输出 1

2

样例输入 2

3 3
888
888
888

样例输出 2

0

样例输入 3

1 1
7

样例输出 3

-1

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.