這是題目:共有7個展開的節點想找出所以的排列組合,但這些節點有優先的先後次序
下例是設計好的題目#(空集合)代表可以優先選擇
B1:#(空集合)
B2:#
B3:(2&7)
B4:(2&7)|(5&7)
B5:(2&7)|(4&7)|(4&6)
B6:(2&7)|(4&7)
B7:(2&4)
說明:1和2沒有約束條件,3受到(2&7)的約束,5受到(2&7)|(4&7)|(4&6)的約束
假設第一步選擇2的節點開始展開,選過可以消掉相關的約束條件如下
B1:#
B3:#(變成空集合)
B4:(5&7)
B5:(4&7)|(4&6)
B6:(4&7)
B7:#
下一步可以做的是1,3,7可以選,假設選1則
B3:#
B4:(5&7)
B5:(4&7)|(4&6)
B6:(4&7)
B7:#
下一步可以做的是3,7可以選,假設選7則
B3:#
B4:#
B5:(4&6)
B6:#
下一步可以做的是3,4,6可以選,假設選6則
B3:#
B4:#
B5:#
下一步可以做的是3,4,5可以選,假設選取的順序為5,4,3
則其中一組的解為2->1->7->6->5->4->3
我希望能用程式自動解出所以解
例如
2->1->7->6->5->3->4
2->1->7->6->4->5->3
2->1->7->6->4->3->5
...
1->2->7->6->5->4->3
...
所以的展開解
請問各位高手我該怎麼做?
2006-12-12 05:23:14 · 1 個解答 · 發問者 EG 1 in 電腦與網際網路 ➔ 程式設計
JJ您好!!
您的程式確實可以做出來我要的,可是choose(s, pic, sel, down, total)
裡面的宣告我看不是很懂,我有寄一封信給您,不知您收到沒?
2006-12-13 07:56:38 · update #1
怕太多字 不敢多做說明
有問題來函問
用遞迴來做
另 依你的說明
限制條件應該寫成 "(2|7)&(5|7)"才對
Private Sub Command6_Click()
Dim s(7) As String, pic As String
Dim sel As Integer, down As Integer, total As Integer
s(1) = ""
s(2) = ""
s(3) = "(2&7)"
s(4) = "(2&7)|(5&7)"
s(5) = "(2&7)|(4&7)|(4&6)"
s(6) = "(2&7)|(4&7)"
s(7) = "(2&4)"
down = 0
total = 0
Call choose(s, pic, sel, down, total)
Text1.Text = Text1.Text & vbCrLf & total
End Sub
Public Sub choose(s() As String, pic As String, sel As Integer, down As Integer, total As Integer)
Dim i As Integer, k As Integer
Dim t(7) As String
If (sel = 5) Then
i = i
End If
For i = 1 To 7
t(i) = s(i)
Next i
k = 1
Do
If (Len(t(k)) = 0) Then
down = 0
t(k) = "x"
If (sel = 6) Then
Text1.Text = Text1.Text & vbCrLf & pic & k
total = total + 1
down = 1
Exit Sub
End If
Call rm_limit(t, CStr(k))
Call choose(t, pic & k & "->", sel + 1, down, total)
If (down = 1) Then
For i = 1 To 7
t(i) = s(i)
Next i
End If
End If
k = k + 1
Loop Until (k > 7)
End Sub
Public Sub rm_limit(s() As String, lim As String)
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 7
j = InStr(s(i), lim)
If (j > 0) Then
If (Len(s(i)) = 5) Then
s(i) = ""
Else
k = InStr(j, s(i), ")")
If (Mid(s(i), k + 1, 1) = "|") Then
s(i) = Left(s(i), k - 5) & Mid(s(i), k + 2)
Else
s(i) = Left(s(i), k - 6) & Mid(s(i), k + 1)
End If
End If
End If
Next i
End Sub
2006-12-12 10:09:14 · answer #1 · answered by JJ 7 · 0⤊ 0⤋