dfs+visited
import java.util.Scanner;
public class Main {
public static void dfs(int[] a, boolean[] vis, int[] tmp, int start){
int len = a.length;
if(start == len){
if(tmp[0] == 0){
return;
}else{
for(int j : tmp){
System.out.print(j + " ");
}
System.out.println();
}
}else{
for(int i=0; i<len; i++){
if(!vis[i]){
tmp[start] = a[i];
vis[i] = true;
dfs(a, vis, tmp, start+1);
vis[i] = false;
}
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strs = str.split(" ");
int a[] = new int[strs.length];
for(int i=0; i<strs.length; i++){
a[i] = Integer.parseInt(strs[i]);
}
int [] tmp = new int[a.length];
boolean [] vis = new boolean[a.length];
dfs(a, vis, tmp, 0);
}
}
dfs+swap
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strs = str.split(" ");
int a[] = new int[strs.length];
for(int i=0; i<strs.length; i++){
a[i] = Integer.parseInt(strs[i]);
}
dfs(a, 0);
}
static void dfs(int a[], int start ){
if(start == a.length){
for(int i :a){
System.out.print(i);
}
System.out.println();
return;
}
for(int i=start; i<a.length; i++){
swap(a, i, start);
dfs(a, start+1);
swap(a, i, start);
}
}
static void swap(int[] a, int i, int j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}