티스토리 뷰
https://www.acmicpc.net/problem/15654
[백준][C++] 15654 N과 M (5)
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
void dfs(int n, int N, int M, vector<int> vec, vector<int> out, vector<int> visit) {
if (n == M) {
for (int i : out) {
cout << i << ' ';
}
cout << '\n';
return;
}
for (int i = 0; i < N; i++) {
if (visit[i]) continue;
out.push_back(vec[i]);
visit[i] = 1;
dfs(n + 1, N, M, vec, out, visit);
out.pop_back();
visit[i] = 0;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
vector<int> vec(N, 0);
for (int i = 0; i < N; i++) {
cin >> vec[i];
}
sort(vec.begin(), vec.end());
vector<int> out;
vector<int> visit(N, 0);
dfs(0, N, M, vec, out, visit);
return 0;
}
'Problem Solving' 카테고리의 다른 글
[프로그래머스][C++] 예상 대진표 (0) | 2022.05.31 |
---|---|
[백준][C++] 15655 N과 M (6) (0) | 2022.05.27 |
[프로그래머스][C++] 소수 찾기 (0) | 2022.05.26 |
[백준][C++] 15652 N과 M (4) (0) | 2022.05.26 |
[백준][C++] 15651 N과 M (3) (0) | 2022.05.26 |
댓글