矩阵 螺旋填充可以提前规定好上下左右四个方向,然后根据所在的位置是否越界或者是否已经被 填充过相应地改变方向。比如 dx=[1,0,-1,0], dy=[0,1,0,-1],设置初试方向 dir=0,每次移动的 时候只要当前位置加上对应的 dx 和 dy,要改变方向的时候就 dir=(dir+1)%4 就可以了,是否 填充满可以直接用填充数量判断。
1105 Spiral Matrix (25 分)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m×n must be equal to N; m≥n; and m−n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than