출처
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15Khn6AN0CFAYD
풀이
#include<bits/stdc++.h>
using namespace std;
string num;
int swapCnt, result;
void dfs(int cnt, int idx) {
if (cnt == swapCnt) {
result = max(result, stoi(num));
return;
}
for (int i = idx; i < num.length() - 1; i++) {
for (int j = i + 1; j < num.length(); j++) {
if (num[i] <= num[j]) {
swap(num[i], num[j]);
dfs(cnt + 1, i);
swap(num[i], num[j]);
}
if (i == num.length() - 2 && j == num.length() - 1) {
swap(num[i], num[j]);
dfs(cnt + 1, i);
swap(num[i], num[j]);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> num >> swapCnt;
result = 0;
dfs(0, 0);
cout << "#" << t << " " << result << endl;
}
}
반응형
'알고리즘' 카테고리의 다른 글
[C++] SWEA [SW 문제해결 기본] 3일차 - String (0) | 2024.11.19 |
---|---|
[C++] SWEA [S/W 문제해결 기본] 1일차 - Flatten (0) | 2024.11.18 |
[C++] SWEA 삼성시의 버스 노선 (0) | 2024.11.16 |
[C++] SWEA 정곤이의 단조 증가하는 수 (0) | 2024.11.16 |
[C++] SWEA 0/1 Knapsack (0) | 2024.11.15 |