관리 메뉴

민우의 코딩노트

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

Algorithm/BOJ

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

미미누 2022. 1. 13. 01:33

[문제]

[풀이]

백트래킹 문제이다!

 

 

[코드]

#include <bits/stdc++.h>

using namespace std;

int n,m;    // 4, 2
int arr[10];
int arrcheck[10];
int check2 = -1;

void start(int k)
{

        
    if(k == m)
    {
        for(int i=0; i<m; i++)
        {
            cout << arr[i] << " ";
        }
        cout << "\n";
        
        return;
        
    }
    for(int i=1; i<=n; i++)
    {
        if(!arrcheck[i])
        {
            arrcheck[i] = 1;
            if(k > 0) check2 = arr[k-1];
            else check2 = -1;
            arr[k] = i;
            if(check2 < arr[k])
                start(k+1); 
            
            arrcheck[i] = 0;
           
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    start(0);
}

'Algorithm > BOJ' 카테고리의 다른 글

백준 15652번 - N과 M (4) / C++  (0) 2022.01.15
백준 15651번 - N과 M (3) - C++  (0) 2022.01.13
백준 9663번 N-Queen / C++  (0) 2022.01.09
백준 1182번 부분수열의 합 - C++  (0) 2022.01.09
백준 15649번 - N과 M(1) - C++  (0) 2022.01.08