QOJ.ac

QOJ

시간 제한: 0.3 s 메모리 제한: 32 MB 총점: 100

#16023. 谜题

통계

Byte-land 的国王收到了一份礼物:一个拼图。该拼图由一个大小为 $n \times n$ 的棋盘组成。位于第 $i$ 行第 $j$ 列($1 \le i, j \le n$)的格子坐标为 $(i, j)$,其中包含一个写有数字 $p(i, j)$ 的拼图块,满足 $1 \le p(i, j) \le n^2$。$1 \dots n^2$ 中的每个数字恰好出现在其中一个拼图块上。

格子坐标示意图

为了解决这个拼图,你需要将拼图块按顺序排列,使得对于每个 $1 \le i, j \le n$,格子 $(i, j)$ 包含写有数字 $j + (i - 1) * n$ 的拼图块。

解决该拼图允许进行以下移动操作:

  • 将某一行中的所有拼图块向右循环移动若干个格子。
  • 将某一列中的所有拼图块向下循环移动若干个格子。

Byte-land 的国王成功解决了他的拼图,但他不确定是否能从任意不同的初始配置开始解决它。请帮助他解决这个问题。

任务

写一个程序:

  • 从标准输入读取拼图初始配置的描述。
  • 判断棋盘上的拼图块是否能仅通过上述移动排好序。如果可以,程序应找到将拼图块排好序的移动序列。
  • 将结果写入标准输出。

输入格式

输入的第一行包含一个整数 $n$ —— 棋盘的边长($2 \le n \le 200$)。

接下来的 $n$ 行包含初始配置的描述。第 $i + 1$ 行包含 $n$ 个整数 $p(i, 1), p(i, 2), \dots, p(i, n)$,用单个空格分隔。

输出格式

如果无解,程序应向标准输出写入仅包含一个单词 NO 的单行。

如果存在解,第一行应包含一个整数 $m$ —— 解决拼图所需的移动步数。移动步数不能超过 $400\,000$。接下来的 $m$ 行应包含移动的描述,每行一个。

每行应由一个字母 R(表示将某一行向右移动)或 C(表示将某一列向下移动)、一个空格以及两个整数 $k$ 和 $l$ 组成,用空格分隔;其中 $1 \le k \le n$,$1 \le l \le n - 1$。

包含 R k l 的行描述了将第 $k$ 行向右循环移动 $l$ 个格子的操作。这样的移动会导致以下棋盘配置:

$$p'(i, j) = \begin{cases} p(i, j + n - l) & \text{若 } i = k \text{ 且 } j \le l \\ p(i, j - l) & \text{若 } i = k \text{ 且 } j > l \\ p(i, j) & \text{若 } i \ne k \end{cases}$$

类似地,包含 C k l 的行描述了将第 $k$ 列向下循环移动 $l$ 个格子的操作。

如果存在多个可能的解,你的程序可以输出其中任意一个。

样例

输入样例 1

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

输出样例 1

2
C 2 1
R 1 3

说明

上述移动序列产生的棋盘配置变化过程如下:

初始状态:

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

执行 C 2 1 后(第 2 列向下循环移动 1 格):

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

执行 R 1 3 后(第 1 行向右循环移动 3 格):

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

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.