Square Grid Puzzle
Time limit1sMemory limit1024 MB
Sort an N by N grid into row-major order using moves that send the top row to the bottom or the left column to the right, in fewer than 3N moves.
- Level
Hard8 of 10
- Topics
- Simulation, Matrix, Implementation
- Solved
- No attempts yet
Problem
In this puzzle, you get a -indexed square grid of distinct integers from to , inclusive. The goal is to reach the ordered state where the number in row and column equals for every . You can reach this goal with two types of moves:
- D move (down): written as "D ", where is a rearrangement of the numbers in the top row. The move removes the top row and adds a new row at the bottom of the grid. The new row holds through from left to right.
- R move (right): written as "R ", where is a rearrangement of the numbers in the leftmost column. The move removes the leftmost column and adds a new column at the right of the grid. The new column holds through from top to bottom.
A rearrangement changes only the order of the numbers, without adding or removing any of them. It may also keep the original order.
For example, suppose the current grid is:
The move "D " produces:
The move "R " instead gives:
For , the target grid is:
You aim to solve the puzzle with fewer than moves. You can still earn partial points with more moves or without solving the puzzle.
Input
The first line contains a single integer . The following lines describe the initial grid, with numbers on each line.
Output
The first line contains a single integer , the number of moves. Each of the following lines contains one move.
Constraints
. Each value of from to has the same number of test cases.