Baekjoon/C

[백준] 15649번 N과 M (1)

달의요정루나 2022. 7. 19. 21:45

https://www.acmicpc.net/problem/15649

 

15649번: N과 M (1)

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

www.acmicpc.net

#include <stdio.h>

int n,m;
int result[1000];
int check[1000];

void DFS(int depth){
    int i;
    if(depth == m){
        for(int i=0; i<m; i++){
            printf("%d ",result[i]);
        }
        printf("\n");
    }
    else {
        for(int i=1;i<=n;i++){
            if(check[i]==0){
                result[depth]=i;
                check[i]=1;
                DFS(depth+1);
                check[i]=0;
            }
        }
    }
}

int main(void)
{
    scanf("%d %d",&n,&m);
    DFS(0);
    return 0;
}