πŸ“Œ 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={})

이런 λŠλ‚ŒμœΌλ‘œ

+ Recent posts