需要有关此Python代码的帮助

时间:2016-03-09 10:49:09

标签: python

我在 http://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html

中找到了这30行代码

请问关于下面2行的请帮助我。我不知道这两行代码在做什么。

for s in solve(X, Y, solution):
            yield s

30行的Knuth X算法:

def solve(X, Y, solution=[]):
    if not X:
        yield list(solution)
    else:
        c = min(X, key=lambda c: len(X[c]))
        for r in list(X[c]):
            solution.append(r)
            cols = select(X, Y, r)
            for s in solve(X, Y, solution):
                yield s
            deselect(X, Y, r, cols)
            solution.pop()

def select(X, Y, r):
    cols = []
    for j in Y[r]:
        for i in X[j]:
            for k in Y[i]:
                if k != j:
                    X[k].remove(i)
        cols.append(X.pop(j))
    return cols

def deselect(X, Y, r, cols):
    for j in reversed(Y[r]):
        X[j] = cols.pop()
        for i in X[j]:
            for k in Y[i]:
                if k != j:
                    X[k].add(i)

0 个答案:

没有答案