#HK4014. 「eJOI2023」Square Grid Puzzle

「eJOI2023」Square Grid Puzzle

Description

In this puzzle, you are given a 00-indexed N×NN \times N square grid consisting of distinct integers from 00 to N×N1N \times N - 1, inclusive. Your goal is to reach the ordered state where the number at the intersection of the ii-th row and the jj-th column is equal to i×N+ji \times N + j for each 0i,j<N0 \leq i, j < N. You can achieve this goal using two types of moves:

  • Down move: "D a[0] a[1]  a[N1]a[0]\ a[1]\ \ldots\ a[N - 1]", where a[0],a[1],,a[N1]a[0], a[1], \ldots ,a[N - 1] is some rearrangement of the numbers from the topmost row of the grid. With this move, the topmost row is removed and the new row created with the numbers a[0],a[1],,a[N1]a[0], a[1], \ldots ,a[N - 1] from left to right is added to the bottom of the grid.
  • Right move: "R b[0] b[1]  b[N1]b[0]\ b[1]\ \ldots\ b[N - 1]", where b[0],b[1],,b[N1]b[0], b[1], \ldots ,b[N - 1] is some rearrangement of the numbers from the leftmost column of the grid. With this move, the leftmost column is removed and the new column created with the numbers b[0],b[1],,b[N1]b[0], b[1], \ldots ,b[N - 1] from top to bottom is added to the right of the grid.

Rearrangement refers to changing the order of the numbers without adding or removing any of them, and it may preserve the original order.

For example, if the current grid is:

Row/Column 0 1 2
0 2 4 6
1 8 1 5
2 7 3 0

By performing the move "D 6 2 4`", we will obtain the following grid:

Row/Column 0 1 2
0 8 1 5
1 7 3 0
2 6 2 4

However, if we instead execute move "R 2 8 7", we would get:

Row/Column 0 1 2
0 4 6 2
1 1 5 8
2 3 0 7

For N=3N = 3, the target grid would look like this:

Row/Column 0 1 2
0 0 1 2
1 3 4 5
2 6 7 8

You aim to solve the puzzle with fewer than 3×N3 \times N moves. However, partial points may be awarded in case you use more moves or not solve the puzzle. Refer to the scoring section for details.

Input Format

The first line contains a single integer: NN.

The following NN lines describe the initial grid, with NN numbers on each line.

Output Format

The first line should contain a single integer, MM, the number of moves. Each of the following MM lines should contain a single move.

Scoring

Let’s denote MM as the amount of moves in your solution. Additionally, define A=3×NA = 3 \times N and B=2×N2B = 2 \times N^2.

If your output is invalid, or if M>BM > B, you receive 00 points. Otherwise, your score depends on the amount of numbers in the correct target positions (denoted as CC).

If C<N×NC < N \times N the puzzle is not solved and you will only receive (50×CN×N)%(50 \times \frac{C}{N\times N})\% of points for a test.

Otherwise:

  • If M<AM < A, you will receive 100%100\% of points for a test.
  • If AMBA \leq M \leq B, you will receive (40×(BMBA)2+50)%(40 \times \left(\frac{B-M}{B-A}\right)^2 + 50)\% of points for a test.
3
1 4 2
3 7 5
6 8 0
4
R 3 6 1
D 2 3 4
D 5 6 7
R 2 5 8

This solution achieves the desired result in fewer than 99 moves, earning the full points.

2
2 1
0 3
0

The puzzle isn't solved since only two numbers (11 and 33) out of 44 are in the correct position. This output would receive 50×24=25%50 \times \frac{2}{4} = 25\% of points for a test.

Constraints

  • 2N92 \leq N \leq 9

Subtasks

  • There are no subtasks.
  • There is an equal number of cases for each NN from 22 to 99.