코린이 탈출기
[알고리즘][C++] 순열, 조합, 부분집합 본문
- 순열
void Permutation(int now_c, int goal_c, vector<int> arr)
{
if (now_c == goal_c)
{
//순열 생성 완료
cout << "{ ";
for (int i = 0; i < set.size(); i++)
cout << set[i] << " ";
cout << "}\n";
return;
}
for (int i = 0; i < dist.size(); i++)
{
if (!visited[i]) {
set.push_back(arr[i]);
visited[i] = 1;
Permutation(now_c + 1, goal_c, arr);
set.pop_back();
visited[i] = 0;
}
}
}
- 조합
void Combination(int now_c, int goal_c, int prev, vector<int> arr)
{
if (now_c == goal_c)
{
//조합 생성 완료
cout << "{ ";
for (int i = 0; i < set.size(); i++)
cout << set[i] << " ";
cout << "}\n";
return;
}
for (int i = prev + 1; i < arr.size(); i++)
{
set.push_back(arr[i]);
Combination(now_c + 1, goal_c, i, arr);
set.pop_back();
}
}
- 부분집합
void Subset(int now_c, int goal_c, vector<int> arr)
{
if (now_c == goal_c)
{
cout << "{ ";
for (int i = 0; i < set.size(); i++)
cout << set[i] << " ";
cout << "}\n";
return;
}
if (!visited[now_c]) {
set.push_back(arr[now_c]);
visited[now_c] = 1;
Subset(now_c + 1, goal_c, arr);
set.pop_back();
visited[now_c] = 0;
Subset(now_c + 1, goal_c, arr);
}
}
'알고리즘 정리' 카테고리의 다른 글
[Python] 위상 정렬 (2) | 2020.09.25 |
---|---|
[Python] 이분 탐색 (0) | 2020.09.23 |