-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15650.java
More file actions
52 lines (42 loc) · 1.38 KB
/
15650.java
File metadata and controls
52 lines (42 loc) · 1.38 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.*;
import java.util.*;
public class Main {
public static int[] arr;
public static boolean[] visited;
public static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
arr = new int[M];
visited = new boolean[N];
dfs(N, M,0);
System.out.println(sb);
}
public static void dfs(int N, int M, int depth) {
if (depth == M) {
for (int val : arr) {
sb.append(val).append(' ');
}
sb.append('\n');
return;
}
for (int i = 0; i < N; i++) {
if (!visited[i]) {
if (depth == 0){
visited[i] = true;
arr[depth] = i + 1;
dfs(N, M, depth + 1);
visited[i] = false;
}
else if (depth > 0 && i > arr[depth - 1] - 1){
visited[i] = true;
arr[depth] = i + 1;
dfs(N, M, depth + 1);
visited[i] = false;
}
}
}
}
}