NOI Contest Archive

Archive / 2023 / Final Round — Day 1

Number Transmutation Quest

Medium · National Olympiad in Informatics Sri Lanka 2023 - Day 1

Once upon a time, in the land of Numeria, there existed a mysterious power known as Matrix Transmutation. Legend had it that this power could transform matrices by rearranging their elements according to a specific set of rules.

In Numeria, there lived a young mathematician named Alex. Known for their exceptional problem-solving skills, Alex was chosen to embark on a quest to master the art of Matrix Transmutation. The ultimate goal of this quest was to transmute a set of matrices into new formations, adhering to the rules of transmutation.

Alex’s first challenge was presented by the wise sage of Numeria, who handed them a set of matrices. Each matrix had a unique dimension, denoted by the number of rows (R) and columns (C). These matrices were populated with distinct integers ranging from 1 to RxC, inclusive.

The task assigned to Alex was to form new matrices, following the rules of Matrix Transmutation. These rules were straightforward but demanded careful arrangement of numbers. According to the rules, if two integers in a matrix shared a common side (either horizontally or vertically), they must not share a common side in the new matrix.

Eager to prove their skills, Alex studied the matrices intently, pondering over the possible arrangements. With each matrix, they had to devise a new formation that satisfied the rules of transmutation. However, Alex knew that sometimes it might be impossible to achieve a valid arrangement given the constraints.

Alex’s journey was filled with multiple test cases, each consisting of a matrix to transmute. The wise sage challenged them with various dimensions, presenting matrices of different sizes. Alex needed to come up with a valid transmutation for each matrix or declare it impossible by outputting -1.

Input Format

First line contains a single integer T - the number of test cases

For each test case:

Output Format

Limits

Constraints

For each test case:

Sample Input 0

1
3 3
1 2 3
4 5 6
7 8 9

Sample Output 0

6 1 8
7 5 3
2 9 4