basic-algo-lecture/workbook/0x0C.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x0C.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip> // setprecision을 사용하기 위한 헤더
#include <climits>
#include <list>
using namespace std;
int n, m;
int curNums[8]; // max 가 8
int nums[8]; // max 가 8
void Funct(int functM, int prev) {
if (m == functM) {
for (int i = 0; i < m; i++)
cout << curNums[i] << " ";
cout << "\n";
return;
}
set<int> curUseNums; // 현재 단계에서 다음 단계로 갈 때 쓴 애들 목록(목록에 있으면 안 쓸 것)
// 아직 수열이 다 안 찼으면 재귀..
for (int i = 0; i < n; i++) {
// 만약 집합에 있으면 패스
// 또는 해당 인덱스 요소를 이미 사용했으면 패스
if (curUseNums.find(nums[i]) != curUseNums.end()) continue;
if (prev > nums[i]) continue;
curNums[functM] = nums[i];
curUseNums.insert(nums[i]); // 집합에 넣기
Funct(functM + 1, nums[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> nums[i];
sort(nums, nums + n); // 오름차수 정렬
Funct(0, 0);
return 0;
}
음.. 수열을 오름차순으로 구성해야 하므로 이전 값이 현재 값보다 크면 그냥 패스하도록 했다. 드디어 N 과 M 시리즈가 끝났다 ^~^
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/016.gif)
참고자료:
BaaaaaaaarkingDog | [실전 알고리즘] 0x0C강 - 백트래킹
[실전 알고리즘] 0x0C강 - 백트래킹
이번에는 백트래킹을 배워보도록 하겠습니다. 백트래킹도 재귀와 더불어 많은 사람들이 고통을 호소하는 알고리즘 중 하나이지만 의외로 그 재귀적인 구조에 매료되어서 참재미를 알아버리는
blog.encrypted.gg