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;
}
'백준 문제풀이 > Bruteforcing' 카테고리의 다른 글
[C++] 백준 문제풀이 (Bruteforcing) 16198번 에너지 모으기 (0) | 2023.07.01 |
---|---|
[C++] 백준 문제풀이 (Bruteforcing) 17086번 아기 상어 2 (0) | 2023.07.01 |
[C++] 백준 문제풀이 (Bruteforcing) 14225 부분수열의 합 (0) | 2023.06.28 |
[C++] 백준 문제풀이 (Bruteforcing) 2502 떡 먹는 호랑이 (0) | 2023.06.27 |
[C++] 백준 문제풀이 (Bruteforcing) 1058번 친구 (0) | 2023.06.27 |