Python代码
n=int(input())
sig=[True for _ in range(n+1)]
arr=[]
dg=[True for _ in range(2*n+1)]
udg=[True for _ in range(2*n+1)]
res=0
def dfs(x):
global res
if x>n:
res+=1
if res<=3:
print(*arr)
return
for i in range(1,n+1):
if sig[i] and dg[i-x+n]and udg[x+i]:
arr.append(i)
sig[i]=False
dg[i-x+n]=False
udg[x+i]=False
dfs(x+1)
arr.pop()
sig[i]=True
dg[i-x+n]=True
udg[x+i]=True
dfs(1)
print(res)