QOJ.ac

QOJ

Limite de temps : 1.0 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#12835. 海军上将

Statistiques

假设你是一支著名海军部队的指挥官。这支部队由 21 艘战舰组成,共有 6 种战舰。首先,有一艘指挥舰,指挥官必须在其中,用整数 0 表示。其他种类的战舰分别用整数 1 到 5 表示,这些种类的战舰数量分别为 2、3、4、5 和 6 艘。

部队即将与敌人作战,因此战舰的正确排列非常重要。战场的形状如下图所示。为简化起见,我们认为所有战舰都具有相同的矩形形状。

幸运的是,战舰的最佳排列方式已经确定。如图所示,战场由 6 行组成。在最佳排列中,所有编号为 $i$ 的战舰必须位于第 $i$ 行(行号从 0 开始编号)。

输入给出了战场的初始状态。战舰占据了 21 个所需的位置,但其中一些可能位于错误的行中。你可以通过交换指挥舰与相邻战舰的位置来改变战场的状态。两艘战舰仅在它们不在同一行,但共享部分边缘时才被视为相邻。例如,如果我们用 $(i, j)$ 表示第 $i$ 行从左往右第 $j$ 个位置的单元格,那么单元格 $(2, 1)$ 与单元格 $(1, 0)$、$(1, 1)$、$(3, 1)$ 和 $(3, 2)$ 相邻(此处行号和位置编号均从 0 开始)。你的任务是求出达到最佳排列所需的最少交换次数,如果需要超过 20 次交换,则报告无法完成。

输入格式

第一行包含一个整数 $T$,表示测试用例的数量($1 \le T \le 10$)。

每个测试用例包含 6 行。每个测试用例的第 $i$ 行包含 $i+1$ 个以空格分隔的整数,表示战场第 $i$ 行从左到右的战舰种类。

输出格式

对于每个测试用例,如果无法在 20 次或更少的交换内达到目标,请在单独的一行中打印 “too difficult”。否则,打印一个整数:最少可能的交换次数。

样例

输入 1

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

输出 1

3

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.