spiral123
Time limit1sMemory limit64 MB
Given n, construct the prescribed n by n spiral123 matrix using the recursive definition from M(n-6) plus a fixed 6 by 6 corner pattern.
- Level
Easy3 of 10
- Topics
- Matrix, Implementation
- Solved
- No attempts yet
Problem
A square matrix is a spiral123 matrix when all three conditions hold.
- Every entry is 0, 1, 2, or 3.
- Every row and every column contains exactly one 1, exactly one 2, and exactly one 3. All other entries are 0.
- Read the entries along the spiral that starts in the upper left corner, runs right along the first row, then turns down, then left, then up, and keeps winding inward. Drop the zeros. The values that remain are 1, 2, 3, 1, 2, 3, and so on, and the last one is 3.

For one there are many spiral123 matrices, so the output section fixes one of them. Given , print that matrix.
Input
The first line contains one integer .
Output
Print lines. Line holds row of the matrix defined below, written as numbers separated by single spaces. Rows and columns are numbered from .
For , is the matrix below. Each block writes one entry as one digit and one matrix row as one line.
12003
30120
20031
03210
01302
123000
301020
000231
010302
032100
200013
1230000
3010020
0000231
0103002
0021300
0302100
2000013
12300000
30100020
00200031
00010302
00032100
01003200
03021000
20000013
000000123
200000031
120003000
003100002
012030000
031002000
000321000
300000210
000210300
0012300000
0003120000
0020000031
0000000312
0000231000
0000002103
2000013000
0130000200
1300000020
3201000000
For , build from . Let . Start with an matrix of zeros. For every and from to , copy the entry of in row , column into row , column . Then, for every and from to , copy the entry of in row , column into row , column . Every other entry stays .
is a spiral123 matrix for every in the input range.