给定一个画布,比方说 10x10,并给定 3 个矩形/正方形。
画布 = 10x10
矩形 1 = 2x2 矩形 2 = 3x3 矩形 3 = 2x4
我创建了一个递归函数,它循环画布上每个矩形的每个位置,它工作正常。(我已经包含了下面的功能,以防有人想看到它,但我认为没有必要)。
我们可以看到矩形 1 和 2 是不可旋转的,即,如果将它们中的任何一个旋转 90 度,它们基本上是相同的形状。但是矩形 3 是可旋转的。
如何更改/构造循环/递归函数,使其循环每个矩形的每个位置以及每个可能的旋转?
目的是遍历画布上所有可能的形状拟合。
谢谢你的帮助!
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