부족한 부분, 틀린 부분에 대한 피드백은 언제든지 댓글로 남겨주세요:)
https://www.acmicpc.net/problem/15664
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 B15664 {
static int N, M;
static int[] num;
static int[] arr;
static LinkedHashSet<String> set = new LinkedHashSet<String>();
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());
num = new int[N];
arr = new int[M];
st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
num[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(num);
dfs(0, 0);
set.forEach(System.out::println);
}
private static void dfs(int start, int depth) {
StringBuilder sb = new StringBuilder();
if(depth == M) {
for(int val : arr) {
sb.append(val).append(" ");
}
set.add(sb.toString());
return;
}
for(int i=start; i<N; i++) {
arr[depth] = num[i];
dfs(i+1, depth+1);
}
}
}
'알고리즘 > 백트래킹' 카테고리의 다른 글
[백준] 15666번 : N과 M (12) (0) | 2024.07.09 |
---|---|
[백준] 15665번 : N과 M (11) (0) | 2024.07.04 |
[백준] 15663번 : N과 M (9) (2) | 2024.07.02 |
[백준] 15657번 : N과 M (8) (0) | 2024.07.01 |
[백준] 15656번 : N과 M (7) (0) | 2024.06.30 |