Square Grid Puzzle

시간 제한1초메모리 제한1024 MB

요약
서로 다른 정수로 채워진 N x N 격자에서 위쪽 행이나 왼쪽 열을 떼어 순서를 바꿔 반대쪽 끝에 붙이는 연산만으로 행 우선 정렬 상태에 도달하는 방법을 찾는다.
난이도

어려움10점 중 8점

유형
시뮬레이션, 구현, 그리디, 행렬
정답자
아직 제출이 없습니다

문제

In this puzzle, you are given a 00-indexed N×NN \times N square grid consisting of distinct integers from 00 to N×N−1N \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 0≤i,j<N0 ≤ i, j < N. You can achieve this goal using two types of moves:

  • Down move: "D a\[0]a\[0] a\[1]a\[1] …\dots a\[N−1]a\[N - 1]", where a\[0]a\[0], a\[1]a\[1], …\dots, a\[N−1]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\[0], a\[1]a\[1], …\dots, a\[N−1]a\[N - 1] from left to right is added to the bottom of the grid.
  • Right move: "R b\[0]b\[0] b\[1]b\[1] …\dots b\[N−1]b\[N - 1]", where b\[0]b\[0], b\[1]b\[1], …\dots, b\[N−1]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\[0], b\[1]b\[1], …\dots, b\[N−1]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/Column001122
00224466
11881155
22773300

By performing the move "D 66 22 44", we will obtain the following grid:

Row/Column001122
00881155
11773300
22662244

However, if we instead execute move "R 22 88 77", we would get:

Row/Column001122
00446622
11115588
22330077

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

Row/Column001122
00001122
11334455
22667788

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.

입력

The first line contains a single integer: NN.

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

출력

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

제한

  • 2≤N≤92 ≤ N ≤ 9
  • There is an equal number of cases for each NN from 22 to 99.

예제2

  1. 예제 1

    입력
    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
    
  2. 예제 2

    입력
    2
    2 1
    0 3
    
    예상 출력
    0