ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ¼Æ­È±Æ¦C²Õ¦X°ÝÃD

¦^´_ 1# qmi
  1. Sub Test()
  2.     Dim arInput, flagTake
  3.     Dim dResult As Object: Set dResult = CreateObject("scripting.dictionary")
  4.    
  5.     arInput = Range("A1").CurrentRegion.Value
  6.     ReDim flagTake(LBound(arInput, 2) To UBound(arInput, 2)) As Boolean 'true=¨ú ; false=¤£¨ú
  7.    
  8.     backtrack arInput, flagTake, 1, 0, dResult
  9.    
  10.     Range("E:F").ClearContents
  11.     Range("E4").Resize(dResult.count).Value = Application.Transpose(dResult.keys)
  12.     Range("F4").Resize(dResult.count).Value = Application.Transpose(dResult.items)
  13. End Sub

  14. Sub backtrack(ByRef arInput, ByRef flagTake, n As Integer, cost As Double, ByRef dResult As Object)
  15.     Dim i As Integer, strOut As String
  16.    
  17.     If cost > 0 And cost <= 105 Then
  18.         For i = LBound(arInput, 2) To UBound(arInput, 2)
  19.             If Len(strOut) = 0 Then
  20.                 strOut = IIf(flagTake(i), arInput(1, i), "")
  21.             Else
  22.                 strOut = strOut & IIf(flagTake(i), "+" & arInput(1, i), "")
  23.             End If
  24.         Next
  25.         dResult.Add strOut, cost    'push to result
  26.     End If
  27.    
  28.     For i = n To UBound(arInput, 2)
  29.         flagTake(i) = True
  30.         backtrack arInput, flagTake, i + 1, cost + arInput(2, i), dResult
  31.         flagTake(i) = False   '¦^´_flag
  32.     Next
  33. End Sub
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

        ÀR«ä¦Û¦b : ¯¸¦b¥b¸ô¡A¤ñ¨«¨ì¥Ø¼Ð§ó¨¯­W¡C
ªð¦^¦Cªí ¤W¤@¥DÃD