题目链接
回溯 dfs
import java.util.*;
import java.math.*;
public class Main {
public static int[] res,is;
public static List<Integer> l=new ArrayList<Integer>();
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
res=new int[n+1];
is=new int[n+1];
dfs(0,n);
}
public static void dfs(int k,int n){
if(k==n){
p(n);
return;
}
for(int i=1;i<=n;i++){
if(is[i]!=1){
is[i]=1;
res[k+1]=i;
dfs(k+1,n);
is[i]=0;
}
}
}
public static void p(int n){
for(int i=1;i<=n;i++)
System.out.printf("%5d",res[i]);
System.out.println();
}
}