๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/STL๊ณผ BASIC

์ˆœ์—ด, ์กฐํ•ฉ, ์ค‘๋ณต์ˆœ์—ด, ์ค‘๋ณต์กฐํ•ฉ with C++

by Jouureee 2022. 3. 3.

1. ์ˆœ์—ด(Permutation)

์ˆœ์„œ๋ฅผ ๋”ฐ์ง€๊ณ , ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. (์ˆœ์„œO, ์ค‘๋ณตX)

๋ฐฑ์ค€ ๊ด€๋ จ ๋ฌธ์ œ : ๋ชจ๋“  ์ˆœ์—ด

-> ๋ฐฑํŠธ๋ž™ํ‚น ๋ฌธ์ œ, visit ๋ฐฐ์—ด์„ ๋‘ฌ ์ค‘๋ณต์„ ๊ฒ€์‚ฌํ•œ๋‹ค. ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ์›์†Œ์— ๋Œ€ํ•ด ๋˜‘๊ฐ™์ด ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

c++ ์ฝ”๋“œ :

//
//  10974_all_ permutation.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/27.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>


using namespace std;
vector<int> v;
bool visit[9];
int n;

///<algorithm> -> next_permutation(์ˆœ์—ด ์‹œ์ž‘, ์ˆœ์—ด ๋)
/// with recursive 
void make_permu_dfs(){
    if(v.size() == n){
        for(int i = 0; i < n; i++){
            cout << v.at(i) << " ";
        }
        cout << endl;
        return;
    }else{
        for(int i = 1; i <= n; i++){
            if(visit[i] == false){
                visit[i] = true;
                v.push_back(i);
                make_permu_dfs();
                v.pop_back();
                visit[i] = false;
            }
        }
    }
}

int main(){
    cin >> n;
    memset(visit, false, sizeof(visit));
    make_permu_dfs();
    return 0;
}

 

2. ์กฐํ•ฉ(Combination)

์ฃผ๋จธ๋‹ˆ์—์„œ ๊ณต์„ ์—ฌ๋Ÿฌ๊ฐœ ๋ฝ‘๋Š”๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์ˆœ์—ด๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

๋ฐฑ์ค€ ๊ด€๋ จ ๋ฌธ์ œ : N๊ณผM(1)

-> ์ˆœ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ visit ๋ฐฐ์—ด์„ ๋‘ฌ ์ค‘๋ณต์„ ๊ฒ€์‚ฌํ•œ๋‹ค. ํ˜„์žฌ ์›์†Œ ์ง‘ํ•ฉ์— ํ•ด๋‹น ์›์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฑด๋„ˆ๋›ด๋‹ค. (set์„ ์‚ฌ์šฉํ•ด๋„ ๋ ๊ฑฐ ๊ฐ™๋‹ค)

 

c++ ์ฝ”๋“œ :

//
//  15649_N&M.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/09/10.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int N, M;
vector<int> v;
int visit[9] = { false, };
vector<int>::iterator it;

//combination
void make_dfs(){
    if(v.size() == M){
        for(int i = 0; i < M; i++){
            cout << v[i] << " ";
        }
        cout << '\n';
        return;
    }else {
        for(int j = 1; j <= N; j++){
            if(visit[j] == false){
                if(find(v.begin(), v.end(), j) != v.end()) { continue; }
                visit[j] = true;
                v.push_back(j);
                make_dfs();
                v.pop_back();
                visit[j] = false;
            }
        }
    }
}

int main(){
    cin >> N >> M;
    make_dfs();
    return 0;
}

 

3. ์ค‘๋ณต ์ˆœ์—ด

์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ˆ˜์—ด์—์„œ ์›์†Œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค.

๋ฐฑ์ค€ ๊ด€๋ จ ๋ฌธ์ œ : N๊ณผM(3)

-> ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ๋”ฐ๋กœ visit ๋ฐฐ์—ด์„ ๋‘์ง€ ์•Š๋Š”๋‹ค.

 

c++ ์ฝ”๋“œ :

//
//  15651_N&M(3).cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/02/20.
//

#include <iostream>

using namespace std;
int N, M;
int arr[8];
//์ค‘๋ณต ์ˆœ์—ด
void backTracking(int cnt){
    if(cnt == M){ // M๊ฐœ ๋‹ค ๊ณจ๋ž๋‹ค๋ฉด
        for(int i = 0; i < M; i++){
            cout << arr[i] << ' ';
        }
        cout << '\n';
        return;
    }
    
    for(int i = 1; i <= N; i++){
        arr[cnt] = i;
        backTracking(cnt+1);
    }
}

int main(){
    cin >> N >> M;
    backTracking(0);
    return 0;
}

 

4. ์ค‘๋ณต ์กฐํ•ฉ

์ค‘๋ณตํ•ด์„œ ๊ฐ™์€ ์›์†Œ๋ฅผ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฐฑ์ค€ ๊ด€๋ จ ๋ฌธ์ œ : N๊ณผM(4)

-> ์ค‘๋ณต ์ˆœ์—ด๊ณผ ๋‹ค๋ฅด๊ฒŒ num ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‘ฌ์„œ ์ด์ „์— ์‹œ์ž‘ํ•œ ์›์†Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์žฌ๊ท€๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

c++ ์ฝ”๋“œ :

//
//  15652_N&M(4).cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/02/20.
//

#include <iostream>

using namespace std;
int N, M;
int arr[8];

//์ค‘๋ณต ํ—ˆ์šฉ + ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ
void backTracking(int cnt, int num){
    if(cnt == M){
        //์ถœ๋ ฅ
        for(int i =0; i < M; i++){
            cout << arr[i] << " ";
        }
        cout << '\n';
        return;
    }
    
    for(int i = num; i <= N; i++){
        arr[cnt] = i;
        backTracking(cnt + 1, i); // num, i ์ฐจ์ด
    }

}

int main(){
    cin >> N >> M;
    backTracking(0, 1);
    return 0;
}

 

'Algorithm๐Ÿฐ > STL๊ณผ BASIC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Swift TIP ์ •๋ฆฌ.zip  (0) 2022.02.18

๋Œ“๊ธ€