Woburn Challenge 1995

Problem 3: Smallest Latin Square

A square arrangment of numbers such as

1 2 3 4 5
2 1 4 5 3
3 4 5 1 2
4 5 2 3 1
5 3 1 2 4

is a Latin Square because each whole number 1, 2, 3, 4 and 5 appears exactly once in each row and column of the square.

Of all the possible 5x5 Latin squares, the one above is the smallest in the following sense: if the digits are strung together (in rows from top to bottom) the resulting integer,

12345 21453 34512 45231 53124

is the smallest one possible.

Input

On the first line is M, an integer indicating the number of test cases.
On each of the next M lines is an integer N between 2 and 9.

Output

Output the smallest NxN latin square for each test case.

Sample Input

1
5

Sample Output

1 2 3 4 5
2 1 4 5 3
3 4 5 1 2
4 5 2 3 1
5 3 1 2 4

All Submissions
Best Solutions


Point Value: 10
Time Limit: 2.00s
Memory Limit: 16M
Added: Sep 29, 2008

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

how do u use it?

that's for you to figure out.

and you too apparently.

I'm guessing you're not allowed to hardcode each individual
square?

more of a challenge.

Unless he does know recursion, but his solution is very inefficient and takes over 2 seconds. Then he could simply let it run for a minute, hard code it, and he's done. So it's a fair question.

It's funny because Brian hard-coded them. ;)

I think for me it was more of a challenge to hard-code, because I found the solutions by hand.