1  2  3  4 
12 13 14 5
11 16 15 6
10 9  8  7

解决方案 »

  1.   

    如果规律如此的话,没有什么困难:dim arr(1 to 16) as integer......
    debug.print arr(1) & vbtab & arr(2) & vbtab & arr(3) & vbtab & arr(4)
    debug.print arr(12) & vbtab & arr(13) & vbtab & arr(14) & vbtab & arr(5)
    debug.print arr(11) & vbtab & arr(16) & vbtab & arr(15) & vbtab & arr(6)
    debug.print arr(10) & vbtab & arr(9) & vbtab & arr(8) & vbtab & arr(7)
      

  2.   

    递归.
    Private Sub Command1_Click()
    Dim result() As Long, i As Long, maxlen As Long
    For i = 1 To 18
    maxlen = Len(CStr(i * i)) + 1
    spiral i, result
    For j = 0 To i ^ 2 - 1
    If j Mod i = 0 Then Debug.Print
    Debug.Print Right(Space(maxlen) & result(j), maxlen);
    Next
    Debug.Print
    Next
    End SubSub spiral(ByVal n As Integer, ByRef result() As Long)
    Dim temp() As Long, i As Long, j As Long
    If n = 1 Then
    ReDim result(0)
    result(0) = 1
    End IfIf n = 2 Then
    ReDim result(3)
    result(0) = 1
    result(1) = 2
    result(2) = 4
    result(3) = 3
    End IfIf n > 2 Then
    ReDim result(n ^ 2 - 1)
    For i = 1 To n - 1
    result(i - 1) = i
    result(i * n - 1) = i + n - 1
    result(n * n - i) = i + 2 * (n - 1)
    result(n * n - i * n) = i + 3 * (n - 1)
    Next
    spiral n - 2, temp
    For i = 0 To (n - 2) ^ 2 - 1
    result(n + 1 + n * (i \ (n - 2)) + i Mod (n - 2)) = temp(i) + 4 * (n - 1)
    NextEnd IfEnd Sub
      

  3.   

    填充如此一个n*n阵列 ,先观察规律:n=1 
    1n=21 2
    4 3
    对于n*n阵列,可以先将1-4*n 填充四周,内部用一个(n-2)*(n-2) 的阵列加上4*n填充,所以用递归比较直观.
      

  4.   

    巧妙!
    我开始以为可以用DFS来做,但后来发现过程中四个方向的优先级不是固定的,DFS不合适。
      

  5.   

    谢谢夸奖.其实不用递归,直接赋值亦可:Private Sub Command1_Click()
    spiral 17
    Debug.Print
    spiral 18
    End SubSub spiral(ByVal n As Integer)
    Dim temp() As Long, i As Long, j As Long, start As Long, maxlen As Long
    ReDim temp(1 To n, 1 To n)
    If n Mod 2 = 1 Then temp(n \ 2 + 1, n \ 2 + 1) = n ^ 2
    For i = 1 To n \ 2
    start = 4 * (i - 1) * (n + 1 - i)
    For j = i To n - i
    temp(i, j) = start + j - i + 1
    temp(j, n + 1 - i) = start + (n - 2 * i + 1) + j - i + 1
    temp(n + 1 - i, n + 1 - j) = start + 2 * (n - 2 * i + 1) + j - i + 1
    temp(n + 1 - j, i) = start + 3 * (n - 2 * i + 1) + j - i + 1
    Next
    Next
    maxlen = Len(CStr(n * n)) + 1
    For i = 1 To n
    For j = 1 To n
    Debug.Print Right(Space(maxlen) & temp(i, j), maxlen);
    Next
    Debug.Print
    Next
    End Sub
      

  6.   

    不客气,找到规律就好了。
    我后来想想DFS也可以,类似于走迷宫一路左转到底,不过还是你的简单明了,用DFS小题大作了。呵呵。