class Node(object):
def __init__(self, v):
self.v = v
self.childs = []
self.parent = None

def addchild(self, child):
child.parent = self
self.childs.append(child)



def get_unit(base, node, anwser):
if(len(base)==0):
result = []
result.append(node.v)
parent = node.parent
while( parent != None ):
result.append(parent.v)
parent = parent.parent


result.reverse()
if(result not in anwser):
anwser.append(result)



for i, v in enumerate(base):
if( node != None):
child = Node(v)
node.addchild(child)
remain = list(base)
remain.pop(i)
get_unit(remain, child, anwser)
else:
root = Node(v);
remain = list(base)
remain.pop(i)
get_unit( remain, root, anwser)


for i in range(10):
for j in range(10):
for k in range(10):
anwser = []
get_unit( [i,j,k], None, anwser)
print "(%d, %d, %d) %s" % (i,j,k,anwser)


结果

(0, 0, 0) [[0, 0, 0]]
(0, 0, 1) [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
(0, 0, 2) [[0, 0, 2], [0, 2, 0], [2, 0, 0]]
(0, 0, 3) [[0, 0, 3], [0, 3, 0], [3, 0, 0]]
(0, 0, 4) [[0, 0, 4], [0, 4, 0], [4, 0, 0]]
(0, 0, 5) [[0, 0, 5], [0, 5, 0], [5, 0, 0]]
(0, 0, 6) [[0, 0, 6], [0, 6, 0], [6, 0, 0]]
(0, 0, 7) [[0, 0, 7], [0, 7, 0], [7, 0, 0]]
(0, 0, 8) [[0, 0, 8], [0, 8, 0], [8, 0, 0]]
(0, 0, 9) [[0, 0, 9], [0, 9, 0], [9, 0, 0]]
(0, 1, 0) [[0, 1, 0], [0, 0, 1], [1, 0, 0]]
(0, 1, 1) [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
(0, 1, 2) [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
(0, 1, 3) [[0, 1, 3], [0, 3, 1], [1, 0, 3], [1, 3, 0], [3, 0, 1], [3, 1, 0]]
(0, 1, 4) [[0, 1, 4], [0, 4, 1], [1, 0, 4], [1, 4, 0], [4, 0, 1], [4, 1, 0]]