QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 2048 MB 満点: 25

#2735. 移动网格

統計

你被给予一个包含编号方块的矩形网格,其中没有空位。该网格只能通过一系列移动操作来改变。移动操作包括将整行向左或向右移动若干个单位,或者将整列向上或向下移动若干个单位。移出矩形边界的方块会从网格的另一侧循环进入。例如,在以下网格中:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

对第二列执行向下移动一个单位的操作,结果如下:

0 13 2 3 4 1 6 7 8 5 10 11 12 9 14 15

注意,向左移动 $K$ 个单位等同于向右移动 $N - K$ 个单位。类似地,向上移动 $K$ 个单位等同于向下移动 $M - K$ 个单位。因此,不失一般性,我们将移动方向限制为仅向右或向下。

在一个有 $N$ 行 $M$ 列的网格中,总共有 $NM$ 个方块。你可以假设方块上标有从 $0$ 到 $NM - 1$ 的不同整数。

你可能已经注意到,在上面给出的第一个例子中,方块处于一种非常有规律的排列中。我们将这种排列称为“已解决”。也就是说,当第一行按顺序包含 $0$ 到 $M - 1$ 的数字,第二行按顺序包含 $M$ 到 $2M - 1$ 的数字,依此类推,最后一行按顺序包含 $(N - 1)M$ 到 $NM - 1$ 的数字时,网格即为已解决。

请找出一系列移动操作,将打乱的网格恢复到已解决的状态。

输入格式

第一行包含两个空格分隔的整数 $N$ 和 $M$ ($2 \le N, M \le 100$)。接下来的 $N$ 行包含 $M$ 个空格分隔的整数,表示网格。

注意 $N$ 和 $M$ 始终为偶数,且存在一个最多需要 $10^5$ 次移动操作的解。

对于 25 分中的 5 分,$N \cdot M \le 8$。 对于另外 25 分中的 10 分,谜题最多可在 2 次移动内解决。

输出格式

输出任何能解决该谜题的移动序列,格式如下:

  • 第一行输出一个整数 $K$ ($0 \le K \le 10^5$),表示序列中的移动次数。
  • 接下来的 $K$ 行,每行格式为 1 i r ($1 \le i \le N, 0 \le r < M$),表示将第 $i$ 行向右移动 $r$ 个单位;或者格式为 2 j d ($1 \le j \le M, 0 \le d < N$),表示将第 $j$ 列向下移动 $d$ 个单位。

样例

样例输入 1

2 4
4 2 3 0
1 5 6 7

样例输出 1

2
2 1 1
1 1 1

说明

我们将第一列向下移动一个单位得到:

1 2 3 0 4 5 6 7

然后将第一行向右移动一个单位达到状态:

0 1 2 3 4 5 6 7

该状态已解决。

样例输入 2

4 2
2 3
5 0
4 1
6 7

样例输出 2

7
1 1 1
2 1 1
1 2 1
1 3 1
2 1 2
1 1 1
2 1 1

说明

从输入开始的移动序列为:

2 3 3 2 6 2 6 2 6 2 1 2 2 1 0 1 5 0 -> 5 0 -> 3 0 -> 0 3 -> 0 3 -> 4 3 -> 4 3 -> 2 3 4 1 4 1 5 1 5 1 1 5 6 5 6 5 4 5 6 7 6 7 4 7 4 7 4 7 0 7 0 7 6 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.