重写树像函数没有递归

时间:2016-06-26 15:24:00

标签: python-3.x recursion

class block(object):
    def __init__(self, N):
        self.N = N; self.l, self.r, self.u, self.d = [None]*4
    def move_lower(self):
        res = []
        for x in (self.u, self.d, self.l, self.r):
            if x != None and x.N < self.N:
                res.append(x)
        return res
    def move_lower_chain(self, res = [], history = []):
        temp = self.move_lower()
        if len(temp) == 0:
            res.append(history + [self.N])
        else:
            for x in temp:
                x.move_lower_chain(res, history + [x.N])
        return res

我试图将'move_lower_chain'函数更改为Non-recursive函数,但我不能甚至不知道如何解决。最困难的部分如下:

        for x in temp:
                x.move_lower_chain(res, history + [x.N])

有没有人可以帮助我?

0 个答案:

没有答案