0

我正在尝试将此递归算法转换为迭代算法,它是 ida A* 算法(来自维基百科)。我已经实现了它,迭代版本不会返回与递归版本相同的结果。

 function search(node, bound)
   if state.f > bound then return state.f
   if is_goal(node) then return FOUND
   min := ∞
   for succ in successors(node) do
     t := search(succ, bound)
     if t = FOUND then return FOUND
     if t < min then min := t
   end for
  return min
 end function

第一次尝试

 function search(node, bound)
   stack = new Stack()
   stack.push(node)
   while(!stack.empty())
     state = stack.pop()
     min := ∞
     if state.f > bound then 
       if state.f < min then min := state.f
       continue
     if is_goal(node) then return FOUND
     for succ in successors(node) do
       stack.push(succ)
     end for
   return min
  end function
4

0 回答 0