Python 递归在块世界问题中不起作用…无法回溯到父节点

Python 递归在块世界问题中不起作用…无法回溯到父节点,python,class,recursion,search,backtracking,Python,Class,Recursion,Search,Backtracking,这里的代码不是回溯到父节点并搜索其他分支。 返回self.children[i].搜索(目标,队列) return self.search(目标、队列)在子节点填满特定分支的队列时发送父节点运行我怀疑这个双重返回是问题的根源: import copy class node: def __init__(self,state): self.state=state self.children=[] def action(self): fo

这里的代码不是回溯到父节点并搜索其他分支。 返回self.children[i].搜索(目标,队列)
return self.search(目标、队列)在子节点填满特定分支的队列时发送父节点运行

我怀疑这个双重
返回
是问题的根源:

import copy
class node:
    def __init__(self,state):
        self.state=state
        self.children=[]
    def action(self):
        for i in range(3):
                for j in range(3):
                    if i!=j:
                        temp=copy.deepcopy(self.state)
                        if len(temp[i])>0:
                            m=temp[i].pop()
                            temp[j].append(m)
                            if temp not in self.children:
                                self.children.append(node(temp))
    def display(self):
        print("different node \n")
        print(self.state)
        for i in range(len(self.children)):
            print(self.children[i].state)
            
    def search(self , goal,queue):
        print('called')
        if self.state==goal:
            print("inspection state=",self.state)
            return True 
        else:
            for i in range(len(self.children)):
                if self.children[i].state not in queue:
                    print(self.children[i].state)
                    self.children[i].action()
                    print(self.children[i].children[0].state)
                    temp=copy.deepcopy(self.children[i].state)
                    queue.append(temp)
                    return self.children[i].search(goal,queue)
                    return self.search(goal,queue)
            else:
                print("queue full error")
                
i_state=[['a'],['b','c'],[]]
root=node(i_state)
root.action()
goal=[[],['a','b','c'],[]]
queue=[]
root.search(goal,queue)
第二条语句将永远不会执行。处理此问题的典型方法可能是:

return self.children[i].search(goal,queue)
return self.search(goal,queue)
或者任何适合你算法的东西。下面是使用此修复程序和一些样式更改的代码:

result = self.children[i].search(goal, queue)

if result:
     return result

return self.search(goal,queue)

欢迎来到SO!这能解决什么问题?“积木字问题”到底是什么?它是关于把积木a、b、c按正确的顺序堆叠起来的。这里有一个链接……谢谢大家!!谢谢。递归对我来说是个新概念,但在可视化流程时遇到了一些问题。我从有序遍历树数据结构的角度出发,想到了将其相邻编写。
import copy

class node:
    def __init__(self, state):
        self.state = state
        self.children = []

    def action(self):
        for i in range(3):
            for j in range(3):
                if i != j:
                    if self.state[i]:
                        temp = copy.deepcopy(self.state)
                        m = temp[i].pop()
                        temp[j].append(m)

                        if temp not in self.children:
                            self.children.append(node(temp))

    def display(self):
        print("different node \n")
        print(self.state)

        for child in self.children:
            print(child.state)

    def search(self, goal, queue):
        print('called')

        if self.state == goal:
            print("inspection state =", self.state)
            return True

        for child in self.children:
            if child.state not in queue:
                print(child.state)
                child.action()
                print(child.children[0].state)
                temp = copy.deepcopy(child.state)
                queue.append(temp)
                result = child.search(goal, queue)

                if result:
                    return result

                return self.search(goal, queue)

        print("queue full error")
        return False

i_state = [['a'], ['b', 'c'], []]
root = node(i_state)
root.action()
goal = [[], ['a', 'b', 'c'], []]
queue = []
root.search(goal, queue)