-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path210129_15664_N과M(10)_s2.java
56 lines (49 loc) · 1.68 KB
/
210129_15664_N과M(10)_s2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int numbers[];
static int visited[];
static LinkedHashSet<String> set = new LinkedHashSet<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
numbers = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; ++i) {
numbers[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(numbers);
visited = new int[N];
int before = 0;
for (int i = 0; i < N; ++i) {
visited[i] = 1;
before = numbers[i];
backTracking(i, 1, Integer.toString(numbers[i])+" ", before);
visited[i] = 0;
}
for (String s: set) {
System.out.println(s);
}
}
static void backTracking(int idx, int cnt, String str, int before) {
if (cnt == M) {
set.add(str);
return;
}
for (int i = 0; i < N; ++i) {
if (numbers[i] < before) continue;
if (visited[i] == 0) {
visited[i] = 1;
backTracking(i, cnt+1, str+Integer.toString(numbers[i])+" ", numbers[i]);
visited[i] = 0;
}
}
}
}