#include <iostream> #include <vector> #include <algorithm> using namespace std; int N, M; int arr[10]; bool isused[10]; void func(int depth){ if(depth == M){ for(int i=0;i < M;i++) cout << arr[i] << ' '; cout << '\n'; return; }else{ for(int i=1;i<=N;i++) { if(!isused[i]){ arr[depth] = i; isused[i] = true; func(depth+1); isused[i] = false; } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> M; func(0); return 0; }
- 백트래킹 문제 유형
: 특정 조건을 만족하는 모든 경우의 수를 수행하는 방법
(경우의 수를 진행하다가 막히면 가장 마지막으로 성공한 부분으로 돌아가 다른 경우로 진행하는 것 -> 백트래킹)