Algorithm

BF / 백준 15665 N과 M 11

Dear-J 2025. 6. 2. 22:03

 

풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class S15665 {
    public static int N, M;
    public static int[] arr, print;
    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());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        arr = new int[N];
        print = new int[M];

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(arr);

        dfs(0);
        System.out.println(sb);
    }

    public static void dfs(int depth) {
        if (depth == M) {
            for (int val : print) {
                sb.append(val).append(" ");
            }
            sb.append("\n");
            return;
        }

        int before = 0;
        for (int i = 0; i < N; i++) {
            if (before != arr[i]) {
                print[depth] = arr[i];
                before = arr[i];
                dfs(depth + 1);
            }
        }
    }
}

'Algorithm' 카테고리의 다른 글

BF / 백준 10972 다음 순열  (0) 2025.06.03
BF / 백준 15666 N과 M 12  (0) 2025.06.02
BF / 백준 15644 N과 M 10  (0) 2025.06.02
BF / 백준 15663 N과 M 9  (0) 2025.06.02
BF / 백준 15656 N과 M 7  (0) 2025.05.31