재귀함수를 통해 경우의 수를 찾아가는 방법은 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);
}
'알고리즘 > Back Tracking' 카테고리의 다른 글
[Back Tracking] 백준 2580번 : 스도쿠 파이썬 (0) | 2021.11.06 |
---|---|
[Back Tracking] 백준 9663번 : NQueen - Python 파이썬 (0) | 2021.11.02 |
Sum-of-Subsets 문제 (0) | 2021.05.12 |
[Back Tracking] 백준 9663번 - N Queen (0) | 2021.01.25 |