DFS 완전탐색 문제다.
횟수와 index를 기준으로 잡고 dfs하면 된다.
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string in;
int out = 0, change_num = 0, len;
void dfs(int ind, int cur)
{
if (cur == change_num)
{
out = max(out, stoi(in));
return;
}
for(int i = ind; i < len - 1; i++)
for (int j = i + 1; j < len; j++)
{
swap(in[i], in[j]);
dfs(i, cur + 1);
swap(in[i], in[j]);
}
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
out = 0;
cin >> in >> change_num;
len = in.length();
if (change_num > len)
change_num = len;
dfs(0, 0);
cout << '#' << test_case << ' ' << out << '\n';
}
return 0;
}
'Coding_Test 연습 > SWEA' 카테고리의 다른 글
[SWEA] (C++) D4 1486. 장훈이의 높은 선반 (0) | 2022.04.12 |
---|---|
[SWEA] (C++) D4 7829 보물왕 태혁 (0) | 2022.04.11 |
[SWEA] (C++) D3 1240 단순 2진 암호코드 (0) | 2022.04.11 |
[SWEA] (C++) D3 1234 비밀번호 (0) | 2022.04.11 |
[SWEA] (C++) D3 1230 암호문3 (0) | 2022.04.11 |