본문 바로가기

백준 문제풀이/Bruteforcing

[C++] 백준 문제풀이 (Bruteforcing) 5568번 카드 놓기

 

https://www.acmicpc.net/problem/5568

 

5568번: 카드 놓기

예제 1의 경우 상근이는 11, 12, 21, 112, 121, 122, 212를 만들 수 있다.

www.acmicpc.net

 

 

//[C++] 백준 문제풀이 (Bruteforcing)

#include <bits/stdc++.h>


std::string numbers[10];
bool used[10];
int n, k;
std::unordered_set<int> data;

void dfs(int cnt, std::string & s){
    if (cnt == k){
        data.insert(std::stoi(s));
        return;
    }

    for (int i = 0; i < n; ++i){
        if (used[i]) continue;
        used[i] = true;
        std::string temp = s;
        s += numbers[i];
        dfs(cnt + 1, s);
        used[i] = false;
        s = temp;
    }
}


int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n >> k;
    for (int i = 0; i < n; ++i){
        std::cin >> numbers[i];
    }
    
    std::string s;
    dfs(0, s);

    std::cout << data.size() << "\n";
    return 0;
}