给定遵循此模式的任何奇数边正方形,通过算法创建其匹配的列表列表

Given any odd sided square following this pattern, algorithmically create its matching list of lists

模式

你只得到一个正方形的边长:

编写一个程序来概括此算法并通过输出列表的列表创建输入方块的列表表示

示例输出

"""
if input = 1    
Matching square:        0         =>  List Equivalent = [[0]]

if input = 3
Matching square:      1 1 1       =>  List Equivalent = [[1, 1, 1],
                      1 0 1                              [1, 0, 1],
                      1 1 1                               [1, 1, 1]]

if input = 5
Matching square:    2 2 2 2 2     =>  List Equivalent = [[2, 2, 2, 2, 2],
                    2 1 1 1 2                            [2, 1, 1, 1, 2], 
                    2 1 0 1 2                            [2, 1, 0, 1, 2],
                    2 1 1 1 2                            [2, 1, 1, 1, 2],
                    2 2 2 2 2                            [2, 2, 2, 2, 2]]

if input = 7
Matching square:  3 3 3 3 3 3 3   =>  List Equivalent = [[3, 3, 3, 3, 3, 3, 3],
                  3 2 2 2 2 2 3                          [3, 2, 2, 2, 2, 2, 3],
                  3 2 1 1 1 2 3                          [3, 2, 1, 1, 1, 2, 3],
                  3 2 1 0 1 2 3                          [3, 2, 1, 0, 1, 2, 3],
                  3 2 1 1 1 2 3                          [3, 2, 1, 1, 1, 2, 3],
                  3 2 2 2 2 2 3                          [3, 2, 2, 2, 2, 2, 3],
                  3 3 3 3 3 3 3                          [3, 3, 3, 3, 3, 3, 3]]

"""

我正在尝试一个解决方案,我将 post 将正方形不均匀地分割为 0,如下所示:

side = 5                                       _ _ _ _ _ _
Matching square:    2 2 2 2 2     Split square: 2 1 0 1 2
                    2 1 1 1 2                   2 1 1 1 2
                    2 1 0 1 2                   2 2 2 2 2        
                    2 1 1 1 2                           
                    2 2 2 2 2 

并找出如何根据与顶行等的距离生成每一行

问题

对于我正在尝试解决的这种模式/问题,有任何现有的解决方案吗?我不知道要搜索什么

一种相对简单但不优雅的方法是将矩阵从外向内螺旋,并在每个新循环中递减该值。如下:

def create_matrix(n): 
   curr = n // 2 
   matrix = [[0]*n for _ in range(n)] 
    
   counter = 0 
   N = n*n 
   left, right, top, bottom = 0, n-1, 0, n-1 
   while True: 
       # top 
       for i in range(left, right+1): 
           matrix[top][i] = curr 
           counter += 1 
       top += 1 
       if counter >= N: 
           break 
       # right 
       for i in range(top, bottom+1): 
           matrix[i][right] = curr 
           counter += 1 
       right -= 1 
       if counter >= N: 
           break 
       # bottom 
       for i in range(left, right+1): 
           matrix[bottom][i] = curr 
           counter += 1 
       bottom -= 1 
       if counter >= N: 
           break 
       # left 
       for i in range(top, bottom+1): 
           matrix[i][left] = curr 
           counter += 1 
       left += 1 
       if counter >= N: 
           break 
       curr -= 1 
   return matrix

快速测试:

In [2]: create_matrix(11)                                                                                              
Out[2]: 
[[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
 [5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5],
 [5, 4, 3, 3, 3, 3, 3, 3, 3, 4, 5],
 [5, 4, 3, 2, 2, 2, 2, 2, 3, 4, 5],
 [5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5],
 [5, 4, 3, 2, 2, 2, 2, 2, 3, 4, 5],
 [5, 4, 3, 3, 3, 3, 3, 3, 3, 4, 5],
 [5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5],
 [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]]

In [3]: create_matrix(5)                                                                                               
Out[3]: 
[[2, 2, 2, 2, 2],
 [2, 1, 1, 1, 2],
 [2, 1, 0, 1, 2],
 [2, 1, 1, 1, 2],
 [2, 2, 2, 2, 2]]

以更简单的方式解决

square_size = 11
centre_x = (square_size - 1) // 2
centre_y = (square_size - 1) // 2

matrix = []
for x in range(square_size):
    row = []
    for y in range(square_size):
        x_dif = abs(x - centre_x)
        y_dif = abs(y - centre_y)
        value = max(x_dif, y_dif)
        row.append(value)
    matrix.append(row)