Little Jay

[C++] 백준 15650번 - N과 M(2) 본문

알고리즘/BOJ

[C++] 백준 15650번 - N과 M(2)

Jay, Lee 2021. 11. 6. 15:36

백트래킹에 들어가있지만,

DFS로 풀 수 있는것 같다.

둘이 조금은 비슷한 성향을 가진 문제인 것 같다.

 

#include <iostream>
#include <cstring>
#define MAX 9
using namespace std;

int n, m;
int arr[MAX] = { 0, };
bool visited[MAX] = { 0, };

void dfs(int num, int cnt) {
    if (cnt == m) {
        for (int i = 0; i < m; i++)
            cout << arr[i] << ' ';
        cout << '\n';
        return;
    }
    for (int i = num; i <= n; i++) {
        if (!visited[i]) {
            visited[i] = true;
            arr[cnt] = i;
            dfs(i + 1, cnt + 1);   
            visited[i] = false;
        }
    }
}

int main() {
    cin >> n >> m;
    dfs(1, 0);

    return 0;
}

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 15652번 - N과 M (4)  (0) 2021.11.06
[C++] 백준 15651번 - N과 M (3)  (0) 2021.11.06
[C++] 백준 7875번 - Brackets  (0) 2021.09.26
[C++] 백준 1712번 - 손익분기점  (0) 2021.09.25
[C++] 백준 10808번 - 알파벳 개수  (0) 2021.09.25
Comments