중복으로 사용할 수 있지만 중복된 수열이 출력되면 안 된다.
배열에서 중복을 제거해 주거나 이전에 사용된 값이면 넘어가는 식으로 해결하면 된다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M;
vector<int> arr, ret;
void dfs(int depth)
{
if (depth == M)
{
for (int i : ret)
cout << i << " ";
cout << "\n";
return;
}
for (int i = 0; i < N; ++i)
{
if (ret[depth] == arr[i])
continue;
ret[depth] = arr[i];
dfs(depth + 1);
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
ret = vector<int>(M);
for (int i = 0, j; i < N; ++i)
{
cin >> j;
arr.push_back(j);
}
sort(arr.begin(), arr.end());
dfs(0);
return 0;
}
중복해서 사용해도 되기에 반복문의 시작을 0부터 해도 된다.