方法1:
visted=[0]*5
n=len(visted)
arr=[]
ans=0
def dfs(index):
global ans
if index==n:
ans+=1
print(arr)
return
for i in range(n):
if visted[i]!=0:
continue
visted[i]=1
arr.append(i+1)
dfs(index+1)
visted[i]=0
arr.pop(-1)
dfs(0)
print(ans)
方法2:
arr=[1,2,3,4,5]
n=len(arr)
ans=0
def dfs(index):
global ans
if index==n:
print(arr)
ans+=1
return
for i in range(index,n):
arr[index],arr[i]=arr[i],arr[index]
dfs(index+1)
arr[index],arr[i]=arr[i],arr[index]
dfs(0)