728x90
반응형
- 풀이 링크:
GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이
알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.
github.com
#include <iostream>
#include <algorithm>
//#include <map> // 중복 x
//#include <string> // getline
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;
/*
[백준 5568 c++ V] 카드 놓기
접근: 순열 -> next_permutation
시간복잡도:
접근2: 순열 -> dfs
풀이:
//1.입력
//2.dfs: 순열 가지수 완탐
//1) 기저: 마지막 숫자 -> set에 푸쉬
//카운트
//중단
//2) 다음 숫자 탐색
개념;
// set<string> cardCnt; 정렬 불필요할때는 unordered가 성능에 유리 (중복제거만 사용)
*/
vector<string> v;
int n, k;
string num;
unordered_set<string> cardCnt;
//unordered_set<string> cardCnt;
// set<string> cardCnt; 정렬 불필요할때는 unordered가 성능에 유리 (중복제거만 사용)
int cnt = 0;
bool visit[15];
void input() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> num;
v.push_back(num);
}
}
void dfs(int cnt, string curPath) {
if (cnt == k) {
cardCnt.insert(curPath);
return;
}
for (int i = 0; i < n; i++) {
if (!visit[i]) {
visit[i] = 1;
dfs(cnt + 1, curPath + v[i]);
visit[i] = 0;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//1.입력
input();
//2.dfs: 순열 가지수 완탐
//1) 기저: 마지막 숫자 -> set에 푸쉬
//카운트
//중단
//2) 다음 숫자 탐색
dfs(0, "");
cout << cardCnt.size() << '\n';
return 0;
}
반응형
'Algorithm_BOJ(백준) > 깊이,너비우선탐색(DFS,BFS)' 카테고리의 다른 글
[백준 5014 c++ V] 스타트링크 (0) | 2023.03.07 |
---|---|
[백준 11724 c++ OOO] 연결 요소의 개수 (0) | 2022.12.31 |
[백준 14940 c++ O] 쉬운 최단거리 (0) | 2022.12.31 |
[백준 1697 c++ VVOO] 숨바꼭질 (0) | 2021.09.15 |
[백준 2606 c++ VOO] 바이러스 (0) | 2021.09.14 |