QOJ.ac

QOJ

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

#15769. 涂色

Estadísticas

有一个由 $n \times n$ 块瓷砖组成的墙壁。很久以前,这些瓷砖被涂成了 $k$ 种不同的颜色。现在油漆老化了,墙壁需要重新粉刷。这一次,墙壁上的所有瓷砖都应该只涂上 $k$ 种颜色中的某一种。

在一步操作中,我们可以粉刷一行(水平)或一列(垂直)的瓷砖。此外,只有当某一行或一列中至少有两块瓷砖已经具有该颜色(无论是旧漆还是新漆)时,我们才能用该颜色粉刷这一行或这一列。

你需要求出粉刷所有瓷砖所需的最少操作次数。

输入格式

输入的第一行包含测试用例的数量。

每个测试用例的第一行包含两个整数:$n$ ($1 < n \le 500$) —— 一行中瓷砖的数量,以及 $k$ ($1 \le k < n$) —— 可用的不同颜色数量。

接下来的 $n$ 行中,每行包含 $n$ 个自然数,表示对应瓷砖之前涂有的颜色编号。颜色的编号为 $1$ 到 $k$。

输出格式

对于每个测试用例,输出的第一行应包含数字 $q$ —— 粉刷所有瓷砖所需的最少操作次数。

第二行按升序输出所有可以按照给定规则、用相同的最少操作次数将整面墙粉刷成该颜色的颜色编号。

如果无法按照题面中的规则粉刷所有瓷砖,则仅输出数字 $0$。

样例

输入样例 1

2
3 2
1 2 1
2 1 1
1 2 2
2 1
1 1
1 1

输出样例 1

4
1
2
1

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.