취미처럼

[백준] 15652번 N과 M (4) 본문

Algorithm/백준

[백준] 15652번 N과 M (4)

sirius 2021. 3. 2. 11:22
https://www.acmicpc.net/problem/15652
 

15652번: N과 M (4)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

 

start를 i와 맞춘다.

 

import java.util.*;
import java.io.*;

public class Main {

    public static int N, M;
    public static int[] arr;
    public static boolean[] visit;
    public static StringBuffer sb = new StringBuffer();

    public static void main(String[] args) throws Exception {
        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[M];
        visit = new boolean[N];
        dfs(0, 0);
        System.out.println(sb);
    }

    private static void dfs(int start, int depth) {

        if (M == depth) {
            for (int num : arr) {
            	sb.append(num).append(" ");
            }
            sb.append("\n");
            return;
        }

        for (int i = start; i < N; i++) {
            visit[i] = true;
            arr[depth] = i + 1;
            dfs(i, depth + 1);
            visit[i] = false;
   		}

    }
}

'Algorithm > 백준' 카테고리의 다른 글

[백준] 15655번 N과 M (6)  (0) 2021.03.03
[백준] 15654번 N과 M (5)  (0) 2021.03.03
[백준] 15651번 N과 M (3)  (0) 2021.03.02
[백준] 15650번 N과 M (2)  (0) 2021.03.02
[백준] 15649번 N과 M(1)  (0) 2021.03.02
Comments