알고리즘
[백준]N와 M(1)
kyeongjun-dev
2020. 4. 25. 20:04
https://www.acmicpc.net/problem/15649
#include<bits/stdc++.h>
using namespace std;
int N, M;
int arr[10];
int isused[10];
void dfs(int depth){
if(depth == M){
for(int i = 0; i<M; i++){
cout<< arr[i] << ' ';
}
cout<< '\n';
return;
}
//루프는 0~2까지
for(int i=0; i<N; i++){
if(isused[i]==0){
arr[depth]=i+1;
isused[i]=1;
dfs(depth+1);
isused[i]=0;
}
}
}
int main(void){
cin>>N>>M;
dfs(0);
}
처음 푼 백트레킹 문제인데, isused[i]=0의 위치를 어디로 해줄지 몰라서 해맴. 재귀는 진짜 이해하기가 넘 힘들다...