[Algorithm Question] Spiral Matrix II (Solving n-order Z-shaped matrix)

1. Raising the question

The characteristic of the n- order Z -shaped matrix is ​​that the elements are arranged in a Z - shaped manner. An n -order Z -shaped matrix means that the size of the matrix is ​​n × n , where n is a positive integer.

Question description

spiral ( Z -shaped) matrix with n rows and  n columns is shown in Figure 1. Observe and find out the filling pattern.  

Figure 1 Spiral ( Z -shaped) matrices with 7 rows and 7 columns and 8 rows and 8 columns

Now given the matrix size  n , please output the matrix.

Input format

Enter an integer  n ( 1 n1000 ) , indicating the matrix size.

Output format

Output n rows and n columns, representing the required matrix.    

Input and output samples

2. Solution ideas

Figure 2 Principle diagram of matrix generation

According to the title description, examples and Figure 2 , it can be seen that the matrix is ​​n × n , in which the elements are arranged from left to right in the even-numbered rows ( the first row is 0 rows ) ; and the elements are arranged in the odd-numbered rows ( the first row is 0 rows ) from right to left.

3. Matrix generation algorithm

n rows and n columns, even-numbered rows ( the first row is row 0 ) generate elements from 0 to n -1 ; odd-numbered rows ( the first row is row 0 ) generate elements from n -1 to 0 .

The program code is as follows:

N = 5
def prt(b):                           # 打印二维列表
    for i in range(N):
        for j in range(N):
            print("%3d" % b[i][j], end='')
        print()

def Helix_MatrixII(n):
    cnt = 1
    for i in range(n):                # 生成n行
        if i % 2 == 0:                # 偶数行
            for j in range(n):        # 由左向右(j为0~n-1)生成
                matrix[i][j] = cnt
                cnt += 1
        else:                         # 奇数行
            for j in range(n):        # 由右向左(j为n-1~0)生成
                matrix[i][n-1-j] = cnt
                cnt += 1

matrix = []                           # 初始化二维矩阵matrix(二维列表)
for i in range(N):
    matrix.append([])
    for j in range(N):
        matrix[i].append(0)
Helix_MatrixII(N)
prt(matrix)

Results of the:

1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

4. Problem Solving Algorithm

According to the question requirements: input the matrix size  n and output the matrix.

def prt(b):                           # 打印二维列表
    for i in range(N):
        for j in range(N):
            print("%3d" % b[i][j], end='')
        print()

def Helix_MatrixII(n):
    cnt = 1
    for i in range(n):                # 生成n行
        if i % 2 == 0:                # 偶数行
            for j in range(n):        # 由左向右(0~n-1)生成
                matrix[i][j] = cnt
                cnt += 1
        else:                   # 奇数行
            for j in range(n):        # 由左向右(n-1~0)生成
                matrix[i][n-1-j] = cnt
                cnt += 1

N = int(input())
matrix = []                           # 初始化二维矩阵matrix(二维列表)
for i in range(N):
    matrix.append([])
    for j in range(N):
        matrix[i].append(0)
Helix_MatrixII(N)
prt(matrix)

Input 8, the output is

Guess you like

Origin blog.csdn.net/hz_zhangrl/article/details/132247924