-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15650.java
More file actions
38 lines (31 loc) · 793 Bytes
/
15650.java
File metadata and controls
38 lines (31 loc) · 793 Bytes
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
import java.util.*;
public class Main {
public static boolean[] visited = new boolean[10];
public static Stack<Integer> result = new Stack<Integer>();
public static int N;
public static int M;
public static void dfs(int depth){
if(depth == M){
for(int x: result)
System.out.print(x + " ");
System.out.println();
return;
}
for(int i = 1; i <= N; i++) {
if (!visited[i] && (result.isEmpty() || i>result.peek())) {
visited[i] = true;
result.push(i);
dfs(depth + 1);
visited[i] = false;
result.pop();
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
M = scanner.nextInt();
dfs(0);
scanner.close();
}
}