我试图在迭代中转换这个递归算法,它是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