十分感谢所有人的参与,这是我的解,与ceoguang兄思路相似。
自内向外:
#include <Array.au3>
Const Enum $RIGHT = 1, $DOWN, $LEFT, $UP, $INVALID_DIRECT
Local $iUBound, $fFlag = TRUE, $iTimer
Local $iX, $iY, $iDirect = $RIGHT, $iStepsRequest = 1, $iStepped
$iUBound = Int(Execute(InputBox("", "Input the number of Spiral bands.")))
If $iUBound < 1 Then Exit
$iTimer = TimerInit()
$iX = BitShift($iUBound, 1) + BitAnd($iUBound, 1) - 1
$iY = $iX
Local $aMatrix[$iUBound][$iUBound]
For $i = 1 To $iUBound ^ 2
$aMatrix[$iX][$iY] = $i
$iStepped += 1
Switch $iDirect
Case $RIGHT
$iY += 1
Case $DOWN
$iX += 1
Case $LEFT
$iY -= 1
Case $UP
$iX -= 1
EndSwitch
If ($iStepped = $iStepsRequest) Then
$iDirect += 1
If $iDirect = $INVALID_DIRECT Then $iDirect = $RIGHT
$iStepped = 0
$fFlag = Not $fFlag
If $fFlag Then $iStepsRequest += 1
EndIf
Next
_ArrayDisplay($aMatrix, TimerDiff($iTimer))
自外向内:
#include <Array.au3>
Local $iUBound, $iFlags = TRUE, $iTimer
Local $iX, $iY, $iDirect = 1, $iStepsRequest = 1, $iStepped
$iUBound = Int(Execute(InputBox("", "Input the number of Spiral bands.")))
If $iUBound < 1 Then Exit
$iTimer = TimerInit()
$iX = BitShift($iUBound, 1)
$iY = $iX - BitAnd(BitNot(BitAnd($iUBound, 1)), 1)
If BitAnd($iUBound, 1) Then
Const Enum $LEFT = 1, $DOWN, $RIGHT, $UP, $INVALID_DIRECT
Else
Const Enum $RIGHT = 1, $UP, $LEFT, $DOWN, $INVALID_DIRECT
EndIf
Local $aMatrix[$iUBound][$iUBound]
For $i = $iUBound ^ 2 To 1 Step - 1
$aMatrix[$iX][$iY] = $i
$iStepped += 1
Switch $iDirect
Case $LEFT
$iY -= 1
Case $DOWN
$iX += 1
Case $RIGHT
$iY += 1
Case $UP
$iX -= 1
EndSwitch
If $iStepped = $iStepsRequest Then
$iDirect += 1
If $iDirect = $INVALID_DIRECT Then $iDirect = 1
$iStepped = 0
$iFlags = Not $iFlags
If $iFlags Then $iStepsRequest += 1
EndIf
Next
_ArrayDisplay($aMatrix, TimerDiff($iTimer))
|