【问题描述】
对于一个 n 行 m 列的表格,我们可以使用螺旋的方式给表格依次填上正整数,我们称填好的表格为一个螺旋矩阵。
例如,一个 4 行 5 列的螺旋矩阵如下:
1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
执行代码:
n, m = (int(x) for x in input().strip().split(' ')) p = [[0]*n for _ in range(m)] left = 0 down = m -1 right = n - 1 up = 0 l_r = n -1 u_d = m -1 num = 1 q = 0 while num < m*n: for i in range(l_r): p[left][i+q] = num num += 1 left += 1 for i in range(u_d): p[i+q][down] = num num += 1 down -= 1 for i in range(l_r, 0, -1): p[right][i+q] = num num += 1 right -= 1 l_r -= 2 for i in range(u_d, 0, -1): p[i+q][up] = num num += 1 up += 1 u_d -= 2 q += 1 print(p)