문제 설명
접근법
정답
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
BackT(0,N,new int[N], new boolean[N+1]);
}
public static void BackT(int depth, int N,int[] answer,boolean[] used) {
if(depth == N) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(answer[i]+" ");
}
System.out.println(sb.toString());
return;
}
for (int i = 1; i <= N; i++) {
if(!used[i]) {
answer[depth] = i;
used[i] = true;
BackT(depth+1,N,answer,used);
used[i] = false;
}
}
}
}