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
- VR
- BFS
- 그래프
- 브루트포스
- Team Fortress 2
- Unreal Engine 5
- 다익스트라
- 스택
- 누적 합
- 구현
- 백준
- 투 포인터
- 자료구조
- XR Interaction Toolkit
- DFS
- 그리디 알고리즘
- 재귀
- 우선순위 큐
- 백트래킹
- 수학
- 정렬
- 유니온 파인드
- 다이나믹 프로그래밍
- 트리
- ue5
- 알고리즘
- 유니티
- 시뮬레이션
- c++
- 문자열
Archives
- Today
- Total
1일1알
백준 15664번 N과 M(10) C++ 본문
https://www.acmicpc.net/problem/15664
15664번: N과 M (10)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
백트래킹을 하는데, 비내림차순으로 하고, 중복을 제거해야한다.
#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 <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m;
vector<int> v;
vector<int> idxes;
vector<bool> visited;
set<vector<int>> st;
void BT(int cnt) {
if (cnt >= m) {
vector<int> tmp;
for (auto a : idxes) {
tmp.push_back(v[a]);
}
st.insert(tmp);
return;
}
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
if (idxes.size() > 0) {
if (v[idxes.back()] > v[i]) continue;
}
visited[i] = true;
idxes.push_back(i);
BT(cnt + 1);
visited[i] = false;
idxes.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
v = vector<int>(n);
visited = vector<bool>(n, false);
for (int i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
BT(0);
for (auto a : st) {
for (auto b : a) {
cout << b << " ";
}
cout << "\n";
}
}
'알고리즘' 카테고리의 다른 글
백준 18404번 현명한 나이트 C++ (0) | 2023.06.04 |
---|---|
백준 1986번 체스 C++ (0) | 2023.06.01 |
백준 20365번 블로그2 C++ (0) | 2023.05.29 |
백준 10972번 다음 순열 C++ (0) | 2023.05.28 |
백준 19942번 다이어트 C++ (0) | 2023.05.27 |