Python递归我遗漏了什么

2024-03-29 00:19:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个罐子的问题。 初始状态为[0,0],每个罐子的容量为[4,9],目标状态为[0,6] 合法动作:将1号或2号罐装满,空1号或2号罐,将其中一个罐放入另一个罐中。在

import search #get the interface
import sys
sys.setrecursionlimit(5000)
class two_jugs(search.Nodes):

    #return the starting state vessel 1: 0, vessel 2: 0
    def start(self):
            return [0,0]

    #returns true if node is equal to goal node
    def goal(self,node):
            return node ==(0,6)
    #yields the successors of the configuration indicated by node
    def succ(self,node):             
            # set capacities for vessel 1: 4, vessel 2 : 9
            c = [4,9];
            #stop at second last
            for i in range(len(node)-1):
                    #create a safety copy
                    new_node = node[:]

                    #fill vessel 1
                    if new_node[i]<=0:
                            new_node[i]=c[i]
                            print new_node

                    #fill vessel 2
                    if new_node[i+1]<=0:
                            new_node[i+1]=c[i+1]
                            print new_node

                    #dump vessel i+1
                    if (new_node[i+1]>0):
                            new_node[i+1]=0
                            print new_node

                    #poor vessel i to vessel i+1                
                    if (new_node[i+1]<c[i+1] and new_node[i]>0):
                            #calculate the difference
                            d = min(new_node[i],c[i+1]-new_node[i+1])
                            new_node[i]= new_node[i]-d
                            new_node[i+1]= new_node[i+1]+d
                            print new_node

                    #dump vessel i
                    if (new_node[i]>0):
                            new_node[i]=0
                            print new_node


               #poor vessel i+1 to vessel 1
                    if (new_node[i]<c[i] and new_node[i+1]>0):
                            #calculate the difference
                            d = min(new_node[i+1],c[i]-new_node[i])
                            #set new node
                            new_node[i+1]= new_node[i+1]-d
                            new_node[i]= new_node[i]+d
                            yield new_node
                            print new_node

问题是既然我已经声明了所有的合法移动,为什么我的程序只返回一个合法移动的结果?例如,从开始状态[0,0]开始,当我运行程序时,它返回[4,0]、[0,4]、[0,9]和其他可能的结果,直到递归停止,但不是我的目标状态。 我错过了什么?在

广度优先搜索类:

^{pr2}$

搜索类:

class Nodes:
    def succ(self,n):
        raise Exception, "Successor undefined"
    def start (self):
        raise Exception, "Start undefined"
    def goal (self,n):
        raise Exception, "Goal undefined"

运行程序的类:

import decant
from breadth_first_search import *

dec = decant.Decant()
print breadth_first_search(dec,[[dec.start()]])

Tags: thetoimportselfnodenewsearchreturn
1条回答
网友
1楼 · 发布于 2024-03-29 00:19:09

您的第一个错误是给类2jugs添加标签。python中的变量名(包括类和函数的名称)以及许多其他编程语言都不能以数字开头。因此,将2jugs重命名为two_jugs。在

precise rule for identifiers in python是标识符,必须以大写(A-Z)、小写(a-z)或下划线(_)开头。标识符的以下字符也可以包含数字(0-9)。(规则在Backus-Naur form中指定)。在

相关问题 更多 >