본문 바로가기
알고리즘/Back Tracking

[Back Tracking] 백준 15650번 - N과 M (2)

by 코딩균 2021. 1. 23.

 

재귀함수를 통해 경우의 수를 찾아가는 방법은 N과 M(1) 블로그 포스팅을 참고!

 

 

#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define endl '\n'

int M, N, min_num, cnt1, cnt2;
char input;
bool choosed[10];
int answer[10];

void loop(int stage)
{
    if (stage == M + 1)
    {
        for (int i = 1; i < M+1; i++)
        {
            cout << answer[i] << " ";
        }
        cout << endl;
        return;
    }

    if(answer[stage-1] == N)
        return;

    for(int i=1; i<N+1; i++){
        if(choosed[i]==true)
            continue;
        else if(stage!=1 && answer[stage-1]>=i)
            continue;

        choosed[i]=true;
        answer[stage]=i;
        loop(stage+1);
        choosed[i]=false;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> N >> M;

    for (int i = 0; i < 8; i++)
    {
        choosed[i] = false;
    }
    loop(1);
}