代码:
res = 0
def check(a):
for i in range(9):
if abs(a[i]-a[i+1]) == 1:
return False
return True
def swap(a, x, y):
temp = a[x]
a[x] = a[y]
a[y] = temp
def sort(a, begin, end):
global res
if begin == end:
if check(a):
res += 1
return
for i in range(begin, end):
swap(a, begin, i)
sort(a, begin+1, end)
swap(a, begin, i)
a = [i for i in range(1, 11)]
sort(a, 0, len(a))
print(res)
结果: