### 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)

qinhaotianon Dec 05, 2008 - 11:57:40 pm UTC recursionbleung91on Dec 06, 2008 - 12:33:56 am UTC Re: recursiondAedaLon Dec 13, 2008 - 4:35:08 am UTC Re: Re: recursionseyonvon Nov 03, 2008 - 5:41:14 am UTC hardcodingsquare?

bleung91on Nov 03, 2008 - 7:48:59 pm UTC if you know recursion, hardcoding is probablyDrSaneon Nov 03, 2008 - 11:01:13 pm UTC Unless...DrSaneon Nov 04, 2008 - 1:20:25 am UTCbbi5291on Nov 04, 2008 - 2:34:27 am UTC