Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 재귀
- 브루트포스
- 우선순위 큐
- ue5
- 투 포인터
- 백트래킹
- 시뮬레이션
- DFS
- 알고리즘
- c++
- 정렬
- 구현
- 유니온 파인드
- Team Fortress 2
- 트리
- XR Interaction Toolkit
- VR
- 스택
- BFS
- Unreal Engine 5
- 다익스트라
- 문자열
- 백준
- 수학
- 누적 합
- 다이나믹 프로그래밍
- 그래프
- 그리디 알고리즘
- 유니티
- 자료구조
Archives
- Today
- Total
1일1알
백준 15666번 N과 M (12) C++ 본문
일반적인 백트래킹으로 문제를 해결하였는데, 비내림차순이고 중복이 아닌 것들만 출력하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
set<string> strs;
int n, m;
void dp(const vector<int>& v, int cnt, vector<int> &ans) {
if (cnt >= m) {
string str = "";
for (auto a : ans) {
str += to_string(a) + " ";
}
if (strs.find(str) == strs.end()) {
strs.insert(str);
cout << str << "\n";
}
return;
}
for (int i = 0; i < v.size(); i++) {
if (ans.empty() || ans.back() <= v[i]) {
ans.push_back(v[i]);
dp(v, cnt + 1, ans);
ans.pop_back();
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
vector<int> v(n);
vector<int> ans;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
dp(v, 0, ans);
};
'알고리즘' 카테고리의 다른 글
백준 11403번 경로 찾기 C++ (0) | 2022.04.05 |
---|---|
백준 2239번 스도쿠 C++ (0) | 2022.04.04 |
백준 15663번 N과 M (9) C++ (0) | 2022.04.01 |
백준 2096번 내려가기 C++ (0) | 2022.03.31 |
백준 9935번 문자열 폭발 C++ (0) | 2022.03.28 |