1804. Dancing Disks

时间限制 1000 ms   内存限制 512 MB

Luka has mastered the Towers of Hanoi puzzle and has invented a somewhat similar game with disks and rods. The puzzle consists of $n$ wooden disks of different sizes and $36$ rods. The disks are numbered with integers $1$ through $n$ in the order of increasing size. The rods are organized in $6$ rows numbered $1$ through $6$ top to bottom and $6$ columns numbered $1$ through $6$ left to right. ![](https://citel.bjtu.edu.cn/acm/user-content/220/image/jnGxYKZ2xBCAbCT7fTg32Pqsua4kZoKRHzJxfvMp.png) An example step The puzzle starts with all $n$ disks stacked in arbitrary order on the rod in the upper-left corner. In each step, a player can pick up a stack of one or more disks from the top of a single rod $R$ and transfer them (without reordering) to the top of the rod that is either immediately below $R$ or immediately to the right of $R$. The goal of the game is to have all the disks stacked on the rod in the lower-right corner neatly ordered by increasing size top to bottom. Given the initial order of the disks on the rod in the upper-left corner, find any valid sequence of steps that solves the puzzle. You may assume that a solution always exists.

输入数据

The first line contains an integer $n (1 ≤ n ≤ 40 000)$ — the number of disks. The following line contains a sequence of n different integers $d_1, d_2, ..., d_n (1 ≤ d_k ≤ n)$ — the initial order of disks, bottom to top, on the rod in the upper-left corner.

输出数据

Output $m$ lines where $m$ is the number of steps in your solution. The $k$-th line should contain four tokens $r_k, c_k, p_k, n_k$, describing the $k$-th step in your solution. The tokens should be as follows:

  • $r_k$ and $c_k$ are integers between $1$ and $6$ denoting the row number and the column number of the rod we are picking up the disks from,
  • $p_k$ is an uppercase letter D or R denoting that we are transferring the disks to the rod directly below or directly to the right respectively,
  • $n_k$ is a positive integer denoting the number of disks we are transferring in this step.

All steps have to be valid according to the rules above and solve the puzzle correctly.

样例输入

复制
6
1 6 5 4 3 2 \n
 · · · · · \n

样例输出 special judge

复制
1 1 D 6
2 1 D 6
3 1 D 6
4 1 D 6
5 1 D 6
6 1 R 6
6 2 R 6
6 3 R 6
6 4 R 6
6 5 R 5
6 5 R 1 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n
 · · · \n

样例说明

In the example above, the first $9$ steps simply move all the disks, without reordering, to the rod in row $6$, column $5$ — immediately to the left of the target rod in the lower-right corner. In the following step, the stack of five disks from the top of the rod — $(6, 5, 4, 3, 2)$ bottom to top — are moved to the target rod. Finally, disk $1$ is moved to the target rod obtaining the target bottom-to-top order $(6, 5, 4, 3, 2, 1)$.

提交

请先 登录

© 2026 FAQs Contact About