NOI Contest Archive

Archive / 2022 / March 2022

Not So Safe

Hard · National Olympiad in Informatics Sri Lanka - March 2022

The year is 2077. You are a member of the Valentinos gang, tasked with breaking into Imperial Bank and cracking open a safe. Imperial Bank uses safes built by ‘AllSafe’. AllSafe safes of size n contain a nxn square formation of cells, each containing a unique digit ranging from 1 to n2, which when arranged in the right order unlocks the safe.

However, your gang has received information that AllSafe safes have a glitch in them. A key component of this glitch is Limbo cells. Two cells are in Limbo if the digits inside them differ by 1.

Two cells of a nxn square formation of cells are adjacent to each other when they have a common side, i.e cell (x,y) is adjacent to cells (x,y-1), (x-1,y), (x, y+1) and (x+1, y).

AllSafe safes can be cracked if the cells are arranged such that no two adjacent cells are in Limbo. You are required to find in advance a square matrix nxn cell configuration that will crack the safe of size n. Note that there can be multiple cell configurations that will crack the safe.

Input Format

Output Format

Constraints

Sample Input 0

3
1
2
3

Sample Output 0

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