1-2. SWEA
[SWEA_D3] 1244번 - 최대 상금
asdklfjlasdlfkj
2020. 3. 15. 19:05
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
조건이 붙은 완전탐색 문제다.
dfs 알고리즘으로 풀 수 있다.
단, 단순히 뒤의 숫자가 앞의 숫자보다 같거나 큰 경우에 대해서 진행한다면 시간초과가 난다.
바꾼 뒤에 기준이 되는 베이스 포인트를 넘겨주고 2중 for문의 바깥 루프에서 이 값부터 기준으로 삼아 탐색을 하면 시간초과가 나지 않고 풀 수 있다.
아래는 전체 코드.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string num;
int answer, MaxChg;
void dfs(int idx, int change){
if (change == MaxChg){
answer = max(answer, stoi(num));
return;
}
if (num[j] >= num[i]){
swap(num[i], num[j]);
dfs(i, change + 1);
swap(num[i], num[j]);
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc;
cin >> tc;
for (int T = 1; T <= tc; T++){
cin >> num >> MaxChg;
answer = 0;
dfs(0, 0);
cout << "#" << T << " " << answer << '\n';
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |