-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16397.cpp
73 lines (61 loc) · 1.53 KB
/
16397.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
int n, count;
};
const int INF = 99999;
int transform(int x) {
string str = to_string(x);
for (int i = 0; i < str.size(); i++) {
if (str[i] != '0') {
return x - pow(10, str.size() - 1 - i);
}
}
return x;
}
void solve(){
int N,T,G; cin >> N>>T>>G;
queue<Node> q;
bool visited[100001]; memset(visited, 0, sizeof(visited));
visited[N] = true;
if (N == G) {
cout << 0;
return;
}
q.push({N, 0});
while (q.size()) {
auto [x, count] = q.front(); q.pop();
int next_count = count + 1;
if (next_count > T) continue;
if (x + 1 <= INF) {
int next_x = x + 1;
if (!visited[next_x]) {
if (G == next_x) {
cout << next_count;
return;
}
visited[next_x] = true;
q.push({next_x, next_count});
}
}
if (x * 2 <= INF) {
int next_x = transform(x * 2);
if (!visited[next_x]) {
if (G == next_x) {
cout << next_count;
return;
}
visited[next_x] = true;
q.push({next_x, next_count});
}
}
}
cout << "ANG";
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}