参考知乎答案:手机的九宫格图案解锁总共能绘出多少种图案? - linkwun 的回答 - 知乎
impossible = {13: 2,
46: 5,
79: 8,
17: 4,
28: 5,
39: 6,
19: 5,
37: 5,
31: 2,
64: 5,
97: 8,
71: 4,
82: 5,
93: 6,
91: 5,
73: 5,
}
def func(n):
if not 1 <= n <= 9:
raise ValueError("1 <= n <= 9")
numbers = list(range(1, 10))
result = []
num = random.choice(numbers)
result.append(num)
numbers.remove(num)
for i in range(n-1):
next_numbers = numbers[:]
for next_num in next_numbers[:]:
key = result[-1]*10 + next_num
if key in impossible:
if impossible[key] not in result:
next_numbers.remove(next_num)
num = random.choice(next_numbers)
result.append(num)
numbers.remove(num)
return result