-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1039.cpp
47 lines (42 loc) · 1.04 KB
/
1039.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
int main() {
cin.tie(0);
std::ios_base::sync_with_stdio(0);
string s;
int k;
cin >> s >> k;
int ans = -1;
queue<pair<string, int>> q;
q.push({s, 0});
set<pair<string, int>> vis;
vis.insert({s, 0});
int m = s.length();
while(!q.empty()){
auto[now, cnt] = q.front();
q.pop();
for(int i = 0; i<m; i++){
for(int j = i+1; j<m; j++){
string copy = now;
char tmp = copy[i];
copy[i] = copy[j];
copy[j] = tmp;
if(copy[0] == '0'){
continue;
}
if(cnt+1 == k){
ans = max(ans, stoi(copy));
continue;
}
if(vis.count({copy, cnt+1}) == 0){
vis.insert({copy, cnt+1});
q.push({copy, cnt+1});
}
}
}
}
cout << ans;
}