π 1. μ‘°ν©μ΄λ?
Nκ°μ μμ μ€μμ μ€λ³΅ μμ΄ μμλ₯Ό κ³ λ €νμ§ μκ³ Kκ°λ₯Ό κ³ λ₯΄λ κ²½μ°μ μ.
β
ex) {A, B, C, D} μ€μμ 2κ°λ₯Ό κ³ λ₯΄λ©΄: AB, AC, AD, BC, BD, CD → μ΄ 6κ°μ§.
π 2. μ‘°ν©μ ꡬννλ 2κ°μ§ λ°©μ
β (1) μ¬κ· + λ°±νΈλνΉ λ°©μβ
#include <iostream>
#include <vector>
using namespace std;
int N = 4, K = 2;
vector<int> arr = {1, 2, 3, 4}; // μμ
vector<int> combination;
void comb(int start, int depth) {
//start : μ΄λ² λ¨κ³μμ μ νν μ μλ μΈλ±μ€μ μμ μμΉ / μ€λ³΅ μμ΄ μ ννλλ‘ λ³΄μ₯ν΄μ€
// depth : νμ¬κΉμ§ λͺ κ°λ₯Ό μ ννλμ§ νμΈ(μΉ΄μ΄νΈ)
if (depth == K) { // κΈ°μ 쑰건: Kκ°λ₯Ό λͺ¨λ μ ννμ λ
for (int num : combination) cout << num << " ";
cout << '\n';
return;
}
for (int i = start; i < N; i++) { //rksmdgks tjsxorwl ahen ghcnf
combination.push_back(arr[i]); // iλ²μ§Έ μμλ₯Ό μ ν
comb(i + 1, depth + 1);
// λ€μ μ ν μ§ν (μ€λ³΅ X → i+1λΆν° μμ) λ€μ μ νμ νμ νμ¬λ³΄λ€ λ€μ μμλ§ λ³΄κΈ°(μ€λ³΅λ°©μ§)
combination.pop_back(); // λ°±νΈλνΉ: μ ννλ κ²μ λλλ¦Ό
}
}
int main() {
comb(0, 0);
return 0;
}
κ°μ₯ λ§μ΄ μ°λ μ‘°ν© κ΅¬ν λ°©μ.
comb(start, depth) : νμ¬ μμΉμμ μμν΄μ μ ν κ°μλ₯Ό κ΄λ¦¬.
λ°±νΈλνΉ μΌλ‘ λΆνμν κ°μ§μΉκΈ°λ₯Ό μ½κ² ν μ μμ.
β
μλ₯Ό λ€μ΄ {1,2,3,4} μ€ 2κ°λ₯Ό κ³ λ₯΄λ κ²½μ°:
comb(0,0) → i=0 μ ν: {1}
comb(1,1) → i=1 μ ν: {1,2} → μΆλ ₯
comb(1,1) → i=2 μ ν: {1,3} → μΆλ ₯
comb(1,1) → i=3 μ ν: {1,4} → μΆλ ₯
comb(0,0) → i=1 μ ν: {2}
comb(2,1) → i=2 μ ν: {2,3} → μΆλ ₯
comb(2,1) → i=3 μ ν: {2,4} → μΆλ ₯
comb(0,0) → i=2 μ ν: {3}
comb(3,1) → i=3 μ ν: {3,4} → μΆλ ₯
β
πΏ μ΄ λ°©μμ μ₯μ
μ½λκ° μ§§κ³ κΉλνλ€, λͺ¨λ μ‘°ν©μ μ€λ³΅ μμ΄ μμ±ν μ μλ€
λ°±νΈλνΉμ μ νμ ν¨ν΄μ΄λΌ λ¬Έμ νμ΄μ μμ©μ΄ μ½λ€
β
β (2) next_permutation μ΄μ© (μμ΄ κΈ°λ° μ‘°ν©)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N = 4, K = 2;
vector<int> arr = {1, 2, 3, 4};
vector<int> bitmask(N - K, 0); // 0μ΄ μμ
bitmask.insert(bitmask.end(), K, 1); // 1μ΄ λ€μ
do {
for (int i = 0; i < N; i++) {
if (bitmask[i]) cout << arr[i] << " ";
}
cout << '\n';
} while (next_permutation(bitmask.begin(), bitmask.end()));
return 0;
}
μμ΄ν¨μμ§λ§ μ‘°ν©μ μ½κ² ꡬν μ μμ.
bitmaskμμ 1μ΄ μ νλ μμΉκ° μ‘°ν©μ΄λ€.
λλμ μ‘°ν©μ μμ μμ΄ κ΅¬ν λ λ§€μ° κΉλν¨.
π 3.νμ© μμ
μ‘°ν©μ λ€μ λ¬Έμ μ νμ μμ£Ό νμ©λ©λλ€:
Nκ° μ€ Kκ° μ ν (ν ꡬμ±, λΆλΆμ§ν©, λ©λ΄ μ‘°ν© λ±)
λΈλ£¨νΈν¬μ€ (μμ νμ)
DFS λ°±νΈλνΉ μ΅μ ν λ¬Έμ
BFS μν μ€μΌ λ
π 4. λ½μκ° λ§κΉ κ²°μ νλ©΄μ μ§ννλ μ¬κ·ν¨μ
(λ°±νΈλ νΉλλ! λ§€λ² μ ν/λΉμ νμΌλ‘ λλλ μ΄μ§νΈλ¦¬νν)
#include <iostream>
#include <vector>
using namespace std;
void Combination(vector<char> arr, vector<char> comb, int r, int index, int depth){
/*arr : μ 체 μμλ€ (μ: {a, b, c, d, e})
comb : νμ¬κΉμ§ μ νλ μ‘°ν© κ²°κ³Ό (μ νλ μμλ€μ λ΄μ)
r : μμΌλ‘ λͺ κ° λ λ½μμΌ νλμ§
index : comb 벑ν°μ μ΄λ μμΉμ λ£μ 건μ§
depth : arrμμ λͺ λ²μ§Έ μμλ₯Ό λ³΄κ³ μλμ§*/
if (r == 0)
{
for(int i = 0; i < comb.size(); i++) cout << comb[i] << " ";
cout << endl;
}
else if (depth == arr.size()) return;
// depth == n // κ³μ μλ½λ€κ° r κ°λ₯Ό μ±μ°μ§ λͺ»ν κ²½μ°λ μ΄ κ³³μ κ±Έλ €μΌ νλ€.
else
{
// arr[depth] λ₯Ό λ½μ κ²½μ°
comb[index] = arr[depth]; // νμ¬μμ λ½κΈ°
Combination(arr, comb, r - 1, index + 1, depth + 1);
// νμ¬μμ λ½κ³ -> λ¨μκ°μ r-1 -> combμ μμΉλ₯Ό idx+1μ λ΄μ -> λ€μ depthλ‘ μ§ν
// arr[depth] λ₯Ό λ½μ§ μμ κ²½μ°
Combination(arr, comb, r, index, depth + 1);
// νμ¬μμ μλ½κ³ -> rμ μ§ -> idxκ·Έλλ‘ -> λ€μ depthλ‘ μ§ν
}
}
int main()
{
vector<char> vec = {'a', 'b', 'c', 'd', 'e'}; // n = 5
int r = 3;
vector<char> comb(r);
Combination(vec, comb, r, 0, 0); // {'a', 'b', 'c', 'd', 'e'}μ '5C3' ꡬνκΈ°
return 0;
- a μ ν → b μ ν → c μ ν → [μΆλ ₯]
→ d μ ν → [μΆλ ₯]
→ e μ ν → [μΆλ ₯]
- b μ ν → c μ ν → d μ ν → [μΆλ ₯]
→ e μ ν → [μΆλ ₯]
... λ±λ±
μ΄λ° λλμ
"μ ν/λΉμ ν ν¨ν΄μ νμ₯μ±μ΄ μ’μ (μ: λΆλΆμ§ν©, DFS λ±)"
β
μ΄κ² λ¬΄μ¨ λ§μ΄λ!!!
"λΆκΈ°λ₯Ό λ κ°λ‘ ꡬλΆνλ€" :
μ‘°ν©(Combination) λ¬Έμ λ₯Ό ν λ, λ§€ μμλ§λ€ "λ½μκΉ?" λλ "μ λ½μκΉ?" μ΄λ κ² 2κ°μ§ μ νμ§λ₯Ό λ§€λ² κ°μ§κ² λ¨
μ¦, μ¬κ·κ° λ§€ μμλ§λ€ μ΄λ κ² λΆκΈ°νλκ±°!
β
ex) λ°°μ΄: { 1, 2, 3, 4 } k = 2κ°λ₯Ό λ½κΈ°
|
μ²μ νΈμΆ:
|
λΆκΈ°1: 1μ λ½λλ€
|
λΆκΈ°2: 1μ μ λ½λλ€
|
|
Combination
(v, cbn, k=2, idx=0, depth=0)
|
cbn[0] = 1
kλ₯Ό νλ μ€μ΄κ³ → k=1
depth νλ μ¦κ° → depth=1
|
cbn 건λλ¦¬μ§ μμ
kλ κ·Έλλ‘ 2
depth μ¦κ° → depth=1
|
|
νμ¬ depth=0 → μμ: 1
|
Combination
(v, cbn, k=1, idx=1, depth=1) μ¬κ·νΈμΆ
|
Combination
(v, cbn, k=2, idx=0, depth=1) μ¬κ·νΈμΆ
|
μ΄λ κ² κ³μ μ§ν
κ° λ¨κ³μμ "λ½μκΉ" vs "μ λ½μκΉ" λ‘ λΆκΈ°κ° λ κ°λλ‘ λλ μ§λ€.
κ·Έλμ μ΄κ±Έ "μ΄μ§ νΈλ¦¬μ²λΌ λΆκΈ°" νλ€κ³ λ μ€λͺ ν¨
cbn[idx] = v[depth];
Combination(v, cbn, k-1, idx+1, depth+1); // λΆκΈ°1: λ½μ
Combination(v, cbn, k, idx, depth+1); // λΆκΈ°2: μ λ½μ
μλ°μμΌλ‘!!!
κ·Έλ¦ΌμΌλ‘ νννλ©΄ μ΄λ κ² λ΄λ €κ°λ ꡬ쑰μ
depth=0 (1 μ ν μ¬λΆ)
βββ μ νν¨ (1)
β βββ μ νν¨ (2)
β βββ μ ν μ ν¨ (3)
βββ μ ν μ ν¨ (1)
βββ μ νν¨ (2)
βββ μ ν μ ν¨ (3)
μ΄κ±Έ μ‘°κΈ λ νμ₯ν΄μ
μ‘°ν© μ€μμ ν©μ΄ μ μΌ ν° κ²μ μ°Ύμ λ°ννλ λ°©μμΌλ‘ λ³νν΄λ³΄λ©΄
β
μμμ½λ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Combination(vector<int>& v, vector<int>& cbn, int k, int idx, int depth, int& maxSum) {
if (k == 0) {
int sum = 0;
for (int i = 0; i < cbn.size(); ++i)
sum += cbn[i];
maxSum = max(maxSum, sum);
}
else if (depth == v.size()) {
return;
}
else {
// μ ννλ κ²½μ°
cbn[idx] = v[depth];
Combination(v, cbn, k - 1, idx + 1, depth + 1, maxSum);
// μ ννμ§ μλ κ²½μ°
Combination(v, cbn, k, idx, depth + 1, maxSum);
}
}
int main() {
int N, K;
cin >> N >> K;
vector<int> vec(N);
for (int i = 0; i < N; ++i)
cin >> vec[i];
vector<int> cbn(K);
int maxSum = 0;
Combination(vec, cbn, K, 0, 0, maxSum);
cout << "μ΅λ ν©: " << maxSum << endl;
return 0;
}
|
void Combination (vector<pair<int,int>>&v, int cnt,
int depth, int Cal, int sum_c, int sum_s, int& max_s){
β
if(cnt==0){
max_s = max(sum_s, max_s);
sum_c = 0; sum_s = 0;
}
β
else if (depth == v.size()) return;
//λ°°μ΄ λ€ λμλλ° λ½μκ² λ¨μμ λ
β
else {
sum_s += v[depth].first; // λ¨ΉμΌλ©΄ λνκΈ°
sum_c += v[depth].second;
Combination (v, cnt-1, depth+1, Cal, sum_c, sum_s, max); //λ¨Ήμ
Combination(v, cnt, depth+1, Cal, sum_c, sum_s, max); //μλ¨Ήμ
}
}
|
void Combination (vector<pair<int,int>>&v, int cnt, int depth, int Cal, int sum_c, int sum_s, int& max_s){
β
if(cnt==0){
if(sum_c <= Cal) // μΉΌλ‘리 μ ν 쑰건 μΆκ°
max_s = max(sum_s, max_s);
return;
}
β
if (depth == v.size()) return;
β
// νμ¬ κ²μ μ ννλ κ²½μ°
Combination (v, cnt-1, depth+1, Cal, sum_c + v[depth].second, sum_s + v[depth].first, max_s);
β
// νμ¬ κ²μ μ ννμ§ μλ κ²½μ°
Combination(v, cnt, depth+1, Cal, sum_c, sum_s, max_s);
}
|
μ΄λ κ² νλ©΄ μ¬κ· νΈμΆ μμμ sum_sμ sum_cμ κ°μ μ§μ 건λ리기 λλ¬Έμ
μ΄νμ λ€λ₯Έ κ²½λ‘μμ sum_s, sum_c κ°μ΄ κΌ¬μΌ μ μμ΄.
β
π κ·Έλμ μΌλ°μ μΌλ‘λ "λ§€κ°λ³μλ‘ λκ²¨μ€ κ°μ μ¬κ· νΈμΆ μμμ λ³κ²½λ§ νλ€" λΌκ³ μκ°νλ©΄ μμ ν¨.
β
3οΈβ£ ν΅μ¬ λ³ν μμ½
sum_s, sum_cλ κ³μ λμ λ κ°μ λ겨주κ³
μ¬κ· νΈμΆ νμ κ°μ 볡ꡬν νμ μμ → κΉλ
cnt==0μΌ λλ§ max_s κ°±μ
β
4οΈβ£ μ μ΄λ κ² νλκ°?
μ¬κ·λ "ν¨μ νΈμΆ μ€ν"μ μ¬μ©νλ―λ‘
sum_s, sum_cλ₯Ό λ°κΎΈκ³ λ€μ λλ €λλ κ²λ³΄λ¨ μμ μΈμλ‘ λκΈ°λ κ² μμ μ μ΄κ³ μ€μ νλ₯ λ μ€μ΄λ¬.
β
μ€μ μ½λ©ν
μ€νΈμμ "μ΄λ κ² λ§€κ°λ³μ μ λ¬νλ λ°©μ" μ λ§μ΄ μ.
|
Combination(v, cnt-1, depth+1, Cal, sum_c + v[depth].second, sum_s + v[depth].first, max_s);
β
μ΄λ κ² νΈμΆμ νμ λ
μ΄κ±΄
νμ¬ λ¨κ³μ sum_c, sum_s κ°μλ μν₯μ΄ μλ€.
λμ , μ¬κ·λ‘ λ΄λ €κ°λ λ€μ ν¨μ νΈμΆμλ§ μλ‘μ΄ κ°μ΄ λμ΄κ°λ€.
λ₯Ό μλ―Ένλ€!!!!
β
μλ₯Ό λ€μ΄μ
νμ¬ sum_s = 10, sum_c = 30, v[depth] = {5, 20} λΌκ³ ν΄λ³΄μ.
Combination(v, cnt-1, depth+1, Cal, 30+20, 10+5, max_s);
μ΄ νΈμΆμ → sum_c = 50, sum_s = 15 λ₯Ό λ€μ ν¨μμκ² λ겨μ€
νμ§λ§ νμ¬ ν¨μμ sum_c, sum_sλ μ¬μ ν 30, 10 μν.
β
β μ κ·Έλ΄κΉ?
λ§€κ°λ³μλ‘ λ겨주λ 건 → μλ‘μ΄ λ³΅μ¬λ³Έμ²λΌ λμ΄κ° (κ° μ λ¬ call-by-value)
μ¦, νΈμΆμ΄ λλκ³ λμμ€λ©΄ μλ κ° μ μ§λ¨.
μ΄κ² λ°λ‘ μ¬κ·κ° κ°μ§μΉκΈ°(νμ)μ μ ν μ μλ μ΄μ
β
β λ§μ½ sum_c, sum_sλ₯Ό μ μ λ³μμ²λΌ λ°κΎΈλ©΄?
κ·Έλ μ λ§λ‘ κ³μ λμ λΌμ κΌ¬μ΄κ² λλ―λ‘π
κ·Έλμ μ μ μν λ³κ²½ λμ μ΄λ κ² "ν¨μ μΈμλ‘λ§ λκΈ°λ ν¨ν΄" μ¬μ©
β
++++++)
void Combination (vector<pair<int,int>>&v, int cnt, int depth, int Cal, int sum_c, int sum_s, int& max_s)
μ¬κΈ°μμ max_s μ μ°Έμ‘°μκ° λΆλ μ΄μ !!!
κΈ°λ³Έμ μΌλ‘ μ¬κ·νΈμΆμ νΉμ§
μ¬κ·ν¨μλ κ³μ κΉμ΄ λ€μ΄κ°λ€κ° λ€μ λμμ€λλ°
ν¨μ μμμ μ§μλ³μλ‘ μ μΈλ λ³μλ€μ νΈμΆμ΄ λλλ©΄ μ¬λΌμ Έ.
κ·Έλμ maxSumμ κ·Έλ₯ int maxSumμΌλ‘ μ°λ©΄ → λ§€λ² μλ‘ λ³΅μ¬λ³Έ λ§λ€μ΄μ μ¬κ· νΈμΆν λλ§λ€ λ 립μ μΈ κ°μΌλ‘ μ‘΄μ¬ν¨.
β
β μ°λ¦¬κ° μνλ λμμ?
μ΅λκ°μ μ 체 νμ λμ κ³μ κ°±μ νκ³ μ μ§νλ κ².
μ¦, μ΄λ€ μ¬κ·νΈμΆμμ μ΅λκ°μ΄ κ°±μ λλ©΄ → κ·Έ κ°μ΄ λ°κΉ₯(μ΄μ νΈμΆ, μ 체 νλ‘κ·Έλ¨)μ λ°μλμ΄μΌ ν¨.
β
β μ°Έμ‘°μ (&) λ₯Ό λΆμ΄
ν΄λΉ ν¨μ νΈμΆ μμμ maxSum κ°μ λ³κ²½νλ©΄, μ΄ κ°μ΄ μλ³ΈμΌλ‘ λ°λ‘ λ°μλ¨.
μ¦, ν¨μ νΈμΆλ§λ€ maxSum λ³μκ° κ³΅μ λ¨.
β
β λ§μ½ & μ λΆμ΄λ©΄?
κ° μ¬κ·νΈμΆλ§λ€ maxSumμ 볡μ¬λ³Έμ΄ λ§λ€μ΄μ§
κ° νΈμΆμμ μ΅λκ°μ κ°±μ ν΄λ, λ°κΉ₯μμλ λ°μλμ§ μμ → κ²°κ³Όκ° νμ 0 λμ¬ μλ μμ.
β
|
void Combination(int sum, int maxSum) {
maxSum = max(maxSum, sum);
}
|
void Combination(int sum, int& maxSum) {
maxSum = max(maxSum, sum);
}
|
|
μμ²λΌ int maxSum μ΄λΌλ©΄ → νΈμΆμ΄ λλλ©΄ maxSum κ°±μ μ λ¨.
|
μ΄λ κ² int& maxSum μ΄λΌλ©΄ → νΈμΆ μμμ λ°λ κ°μ΄ κ³μ μ μ§λ¨.
|
μ‘°ν© κ³΅λΆ λ!!!!
β
+ λΆλΆμ§ν© ꡬνλ λ°©λ²
λλ forλ¬ΈμΌλ‘ ν¨μλ₯Ό κ³μ νΈμΆνλλ° μ΄λ¬λ©΄ runtime errerλΈ
#include <iostream>
#include <vector>
using namespace std;
void dfs(vector<int>& arr, int depth, vector<int>& path, int& totalCount) {
/*arr : μ 체 μμλ€ (μ: {1,2,3,4,5,6})
path: μ§κΈκΉμ§ μ νν μμ (λΆλΆμ§ν©)
totalCount: λΆλΆμ§ν©μ κ°μλ₯Ό μΈλ λ³μ
depth : arrμμ λͺ λ²μ§Έ μμλ₯Ό λ³΄κ³ μλμ§*/
if (depth == arr.size()) { // μ¬κ· λλλ μ’
λ£ μ‘°κ±΄ : μ 체 μμλ₯Ό λ€ κ³ λ €νμ λ(=λ§μ§λ§κΉμ§ λλ¬νμ λ)
if (!path.empty()) { //λΆλΆμ§ν©μ΄ 곡μ§ν©μ΄ μλλ©΄ (path.empty() → μμκ° νλλΌλ μμΌλ©΄) cnt μ¦κ° + μΆλ ₯
totalCount++;
for (int num : path) cout << num << " ";
cout << endl;
}
return;
}
// ν¬ν¨νλ κ²½μ°
path.push_back(arr[depth]);
dfs(arr, depth + 1, path, totalCount);
path.pop_back();
// νμ¬ μμ arr[depth] λ₯Ό μ νν¨ (pathμ λ£μ) λ€μ μμ (depth + 1)λ‘ μ΄λ
// μ¬κ·κ° λλκ³ λλ©΄ path.pop_back() μΌλ‘ λ°©κΈ μ νν μμλ₯Ό λλλ¦Ό (λ°±νΈλνΉ)
// μ΄λ κ² μ λλ리면 pathκ° κ³μ κΈΈμ΄μ§ → "μ νμ μ·¨μ" νλ λ¨κ³
// ν¬ν¨νμ§ μλ κ²½μ° -> λ€μμΌλ‘ λμ΄κ°
dfs(arr, depth + 1, path, totalCount);
}
int main() {
int N = 6;
vector<int> arr = {1, 2, 3, 4, 5, 6};
vector<int> path;
int totalCount = 0;
dfs(arr, 0, path, totalCount);
cout << "μ΄ κ²½μ°μ μ: " << totalCount << endl;
return 0;
}
dfs(0) → depth 0 → μμ 1μ ν¬ν¨νλ λ§λ
|
βββ ν¬ν¨ (path={1})
| β dfs(1)
| βββ ν¬ν¨ (path={1,2})
| | β dfs(2)
| | βββ ν¬ν¨ (path={1,2,3}) → μΆλ ₯
| | βββ λ―Έν¬ν¨ (path={1,2}) → μΆλ ₯
| βββ λ―Έν¬ν¨ (path={1})
| β dfs(2)
| βββ ν¬ν¨ (path={1,3}) → μΆλ ₯
| βββ λ―Έν¬ν¨ (path={1}) → μΆλ ₯
βββ λ―Έν¬ν¨ (path={})
β dfs(1)
βββ ν¬ν¨ (path={2})
βββ λ―Έν¬ν¨ (path={})
μ΄λ° λλμΌλ‘
'C++' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| λΉνΈ λ³ννκΈ° (bitset) (0) | 2025.09.13 |
|---|---|
| λ°μ΄ν° νμ± (0) | 2025.06.17 |
| 컀μ€ν λ³μ μ°μ°μ μ€λ²λ‘λ© (0) | 2025.06.17 |
| μ λ ¬μ© λΉκ΅ ν¨μ κ°μ²΄ (0) | 2025.06.17 |
| ν¬μΈν° (λμ€μ λ€μ λ 곡λΆνκΈ°;) (1) | 2025.06.17 |