給定一個畫布,讓我們說10x10,並給出3個矩形/方塊。幫助修改遞歸函數
帆布= 10×10
矩形1 = 2×2矩形 2 = 3×3矩形 3 = 2x4的
我已經創建了循環在畫布上的每個矩形的每一個位置的遞歸函數,和它的工作原理精細。 (我已經包括下面的功能,任何人都想看到它,但我不認爲這是必要的)。
我們可以看到矩形1和2是不可旋轉的IE,如果你將它們中的任何一個旋轉90度,它們本質上是相同的形狀。但矩形3是可旋轉的。
我該如何改變/構造循環/ recurisve函數,以便循環每個矩形的每個位置以及每個可能的旋轉?
目標是循環遍歷畫布上所有可能的形狀。
感謝您的幫助!
Sub recurse(ByVal startPoint As Integer)
Dim x As Integer
Dim y As Integer
Dim validSolution As Boolean = isSolutionValid()
Dim loopXTo As Integer
Dim loopYTo As Integer
Dim solutionRating As Integer
'If parent nodes create invalid solution, we can skip (375 iterations from 1,600 iterations saving)
If validSolution = True Then
If (startPoint = 0) Then
loopXTo = Math.Floor((canvasCols - squareObjects(startPoint).sqRows())/2) '576 iterations from 1,680 iterations
loopYTo = Math.Floor((canvasRows - squareObjects(startPoint).sqCols)/2) '31,104 iterations from 90,720 iterations
Else
loopXTo = canvasCols - squareObjects(startPoint).sqRows
loopYTo = canvasRows - squareObjects(startPoint).sqCols
End If
'Loop all positions on canvas
For x = 0 To loopXTo
For y = 0 To loopYTo
'Set coords of square
squareObjects(startPoint).setSquareCords(x, y)
'Phyiscally place it in canvas
placeSquareOnCanvas(x, y, squareObjects(startPoint).sqRows, squareObjects(startPoint).sqCols)
'Recursive, get next square
If (startPoint + 1 < totalSquares) Then
recurse(startPoint + 1)
Else
validSolution = isSolutionValid()
'Is solution valud
If (validSolution = True) Then
solutions = solutions + 1
End If
iterations = iterations + 1
'Response.Write("<br /><b>Iteration " & iterations & "</b>")
If (validSolution) Then
'Rate solution, record if best
solutionRating = rateSolution()
If solutionRating > bestCellSaving Then
bestCellSaving = solutionRating
copySolution()
End If
'Response.Write(" <span style='color:green'> <B>VALID SOLUTION</B></span> (" & rateSolution() & ")")
End If
'printCanvas(canvas)
End If
squareObjects(startPoint).removeSquare(canvas)
Next
Next
End If
End Sub
我添加了賞金,因爲我還沒有找到解決方案。爲了清楚起見,我試圖循環畫布上每個形狀的每一個位置(已經完成),但是我需要修改它,以便它在每次旋轉時循環每個形狀的每個位置。基本上,每個位置的組合! – 2010-07-01 09:54:37