你想要的是深度優先搜索。
function ExamineField(Field F)
{
if (F.already_in_list)
return
foreach C child of F
{
call ExamineField(C)
}
AddToList(F)
}
然後,只需依次在每個字段上調用ExamineField(),並根據您的規範以最佳順序填充列表。
請注意,如果字段是循環(也就是說,您有類似於A = B + C,B = A + D的東西),那麼必須修改該算法以便它不會進入無限循環。
對於你的榜樣,呼叫會去:
ExamineField(A)
ExamineField(B)
ExamineField(C)
AddToList(C)
ExamineField(E)
AddToList(E)
AddToList(B)
ExamineField(D)
ExamineField(B)
(already in list, nothing happens)
ExamineField(C)
(already in list, nothing happens)
AddToList(D)
AddToList(A)
ExamineField(B)
(already in list, nothing happens)
ExamineField(C)
(already in list, nothing happens)
ExamineField(D)
(already in list, nothing happens)
ExamineField(E)
(already in list, nothing happens)
,榜單最終將C,E,B,d,A
來源
2009-07-28 06:31:47
caf
非常感謝,這正是術語,我之後。 – Coxy 2009-07-28 08:03:30