백준 풀이 C++

백준 15651 N과M(3) (백트래킹)

ag2개발자 2022. 9. 22. 19:27

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ss stable_sort
#define ll long long

int n, m;
int arr[10];   //넉넉하게 10개짜리 배열

void dfs(int x) {     //백트래킹
if (x == m) {     //백트래킹이 어느 조건에 도달했을때
for (int i = 0; i < m; i++) {   //m개를 출력
cout << arr[i] << " ";
}
cout << "\n";
}
else {
for (int i = 1; i <= n; i++) {   //n까지 돌면서
 {   
arr[x] = i;    // 값 저장
dfs(x + 1);    //깊게 들어감
  //방문 풀어줌
}
}
}
}


int main() {
cin.tie(0)->sync_with_stdio(0);
// for(int i=0; i< n;i++)

cin >> n >> m;    // 입력받기
dfs(0);   //0부터 시작 arr는 인덱스가 0부터이기 때문
  // n,m과 상관 x

return 0;
}