[알고리즘] 조합 탐색 정리하기

WIGWAG·2023년 4월 28일

중복이 없는 모든 순서 조합

012를 조합할 때 결과가 다음과 같이 나온다.
012
021
102
120
201
210

    string s = "012";

    do
    {
        cout << s << endl;
    } while (next_permutation(s.begin(), s.end()));

중복이 있는 모든 순서 조합

012를 조합할 때 결과가 다음과 같이 나온다.
000
001
002
010
011
012
020
021
022
100
101
102
110
111
112
120
121
122
200
201
202
210
211
212
220
221
222

string numbers = "012";
string mix = "";

void dfs(int h) {
    if (h == N) {
        cout << "조합 : " << mix << endl;
        return;
    }

    cout << "층 : " << h << endl;

    for (int i = 0; i < N; i++) {
        mix += numbers[i];
        dfs(h + 1);
        mix.pop_back();
    }
}
profile
프로그래밍 공부 기록 노트

0개의 댓글