-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
40 lines (33 loc) Β· 885 Bytes
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Permutation.P10974;
import java.util.Scanner;
public class Main {
static int N;
static int[] nums;
static boolean[] visited;
static int[] ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
nums = new int[N];
visited = new boolean[N];
ans = new int[N];
dfs(0, ans);
}
static void dfs(int count, int[] arr) {
if (count == N) {
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
for (int i = 0; i < N; i++) {
if (!visited[i]) {
visited[i] = true;
arr[count] = i+1;
dfs(count + 1, arr);
visited[i] = false;
}
}
}
}