spiral123

Time limit1sMemory limit64 MB

Summary
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 nn there are many n×nn \times n spiral123 matrices, so the output section fixes one of them. Given nn, print that matrix.

Input

The first line contains one integer nn.

Output

Print nn lines. Line ii holds row ii of the matrix M(n)M(n) defined below, written as nn numbers separated by single spaces. Rows and columns are numbered from 00.

For 5≤n≤105 \le n \le 10, M(n)M(n) is the matrix below. Each block writes one entry as one digit and one matrix row as one line.

M(5)M(5)

12003
30120
20031
03210
01302

M(6)M(6)

123000
301020
000231
010302
032100
200013

M(7)M(7)

1230000
3010020
0000231
0103002
0021300
0302100
2000013

M(8)M(8)

12300000
30100020
00200031
00010302
00032100
01003200
03021000
20000013

M(9)M(9)

000000123
200000031
120003000
003100002
012030000
031002000
000321000
300000210
000210300

M(10)M(10)

0012300000
0003120000
0020000031
0000000312
0000231000
0000002103
2000013000
0130000200
1300000020
3201000000

For n≥11n \ge 11, build M(n)M(n) from M(n−6)M(n-6). Let r=(0,1,2,n−3,n−2,n−1)r = (0, 1, 2, n-3, n-2, n-1). Start with an n×nn \times n matrix of zeros. For every ii and jj from 00 to 55, copy the entry of M(6)M(6) in row ii, column jj into row rir_i, column rjr_j. Then, for every ii and jj from 00 to n−7n-7, copy the entry of M(n−6)M(n-6) in row ii, column jj into row i+3i+3, column j+3j+3. Every other entry stays 00.

M(n)M(n) is a spiral123 matrix for every nn in the input range.

Constraints

  • 5≤n≤2005 \le n \le 200

Examples2

  1. Example 1

    Input
    5
    
    Expected output
    1 2 0 0 3
    3 0 1 2 0
    2 0 0 3 1
    0 3 2 1 0
    0 1 3 0 2
    
  2. Example 2

    Input
    11
    
    Expected output
    1 2 3 0 0 0 0 0 0 0 0
    3 0 1 0 0 0 0 0 0 2 0
    0 0 0 0 0 0 0 0 2 3 1
    0 0 0 1 2 0 0 3 0 0 0
    0 0 0 3 0 1 2 0 0 0 0
    0 0 0 2 0 0 3 1 0 0 0
    0 0 0 0 3 2 1 0 0 0 0
    0 0 0 0 1 3 0 2 0 0 0
    0 1 0 0 0 0 0 0 3 0 2
    0 3 2 0 0 0 0 0 1 0 0
    2 0 0 0 0 0 0 0 0 1 3