def gen_iter(result, digit, pool, pick): if not digit: result.append(pick) else: for i in range(len(pool) - digit + 1): gen_iter(result, digit - 1, pool[i + 1:], pick + pool[i]) def gen_pick(digit, pool): result = [] gen_iter(result, digit, pool, '') return result def judge(state, c1, c2): for p1, p2 in state: if not ((p1 in c1 and p2 in c2) or (p2 in c1 and p1 in c2)): return False return True def search(): state = [('0', '1'), ('0', '4'), ('0', '6'), ('1', '6'), ('2', '5'), ('3', '6'), ('4', '6'), ('6', '4'), ('8', '1')] tale = [] cube = gen_pick(6, '0123456786') for i in range(len(cube)): for j in range(i, len(cube)): if judge(state, cube[i], cube[j]): tale.append((cube[i], cube[j])) return len(tale) print(search())