Under construction

Still being built. Things may change

Dancing Disks

Time limit3sMemory limit512 MB

Summary
Move n numbered disks from the upper-left rod of a 6x6 rod grid to the lower-right rod, shifting stacks only down or right, and print a valid sequence of moves.
Level

Hard9 of 10

Topics
Divide and conquer, Simulation, Array
Solved
No attempts yet

Problem

Luka has mastered the Towers of Hanoi puzzle and invented a similar game with disks and rods. The puzzle has nn wooden disks of different sizes and 36 rods. The disks are numbered 1 through nn in order of increasing size. The rods form 6 rows, numbered 1 through 6 from top to bottom, and 6 columns, numbered 1 through 6 from left to right.

An example step

The puzzle starts with all nn 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 RR and transfer them, without reordering, to the top of the rod immediately below RR or immediately to the right of RR. The goal is to have all the disks on the rod in the lower-right corner, ordered by increasing size from top to bottom.

Given the initial order of the disks on the upper-left rod, find any valid sequence of steps that solves the puzzle. You may assume that a solution always exists.

Input

The first line contains an integer nn (1≤n≤40 0001 \le n \le 40\,000), the number of disks. The next line contains nn distinct integers d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤dk≤n1 \le d_k \le n), the initial order of the disks on the upper-left rod, from bottom to top.

Output

Print mm lines, where mm is the number of steps in your solution. The kk-th line contains four values rkr_k, ckc_k, pkp_k, nkn_k.

  • rkr_k and ckc_k are the row and column of the rod the disks are picked up from, each between 1 and 6.
  • pkp_k is an uppercase letter, "D" or "R", meaning the disks are moved to the rod directly below or directly to the right, respectively.
  • nkn_k is a positive integer, the number of disks moved in this step.

Every step must be valid under the rules above, and the steps must solve the puzzle.

Hint

In the example picture, the first 9 steps move all the disks, without reordering, to the rod in row 6, column 5, which is directly left of the target rod in the lower-right corner. The next step moves the top five disks, which are (6, 5, 4, 3, 2) from bottom to top, to the target rod. Finally, disk 1 is moved to the target rod, giving the bottom-to-top order (6, 5, 4, 3, 2, 1).

Examples1

  1. Example 1

    Input
    6
    1 6 5 4 3 2
    
    Expected output
    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