2011-10-22 54 views

回答

1

我給你第一次深度優先僞碼圖

DLS(node, goal, depth, visited) 
{ 
    if (depth >= 0) 
    { 
    if (node == goal) 
     return node 

    visited.insert(node) 

    for each child in expand(node) 
     if (child is not in visited) 
      DLS(child, goal, depth-1, visited) 
    } 
} 

和迭代DLS是

IDDFS(start, goal) 
{ 
    depth = 0 
    while(no solution) 
    { 
    visited = [] // <-- Empty List 
    solution = DLS(start, goal, depth,visited) 
    depth = depth + 1 
    } 
    return solution 
} 

你總是可以通過去除圖形循環變換一個圖形在樹上一個訪問列表。 :)

+0

重要的是要注意,迭代加深例程中的每個DLS調用都以**新鮮的關閉列表開始;否則,第二次和以後的呼叫根本不會搜索。 –

+0

答案不是關於迭代縮減,你的描述不符合你給出的代碼,或者我錯了? – wmax