-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation_from_a_to_b.cpp
85 lines (57 loc) · 1.25 KB
/
transformation_from_a_to_b.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
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
vector<int> adj;
map<int, int> func;
bool bfs(int in, int out){
queue<int> q;
int count = 0;
q.push(in*10+1);
q.push(in*2);
adj.push_back(in);
func[in*10+1] = count++;
adj.push_back(in);
func[in*2] = count++;
while(!q.empty()){
auto v = q.front();
q.pop();
if(v < 0 or v > out)
continue;
adj.push_back(v);
func[v*10+1] = count++;
adj.push_back(v);
func[v*2] = count++;
if(v == out)
return true;
q.push(v*10 + 1);
q.push(v * 2);
}
return false;
}
vector<int> path (int in, int out){
vector<int> ans;
auto p = out;
ans.push_back(out);
while(p != in){
p = adj[func.at(p)];
ans.push_back(p);
}
reverse(ans.begin(), ans.end());
return ans;
}
int main(int argc, char const *argv[])
{
int a, b;
cin >> a >> b;
if (bfs(a, b)){
vector<int> ans;
ans = path(a, b);
cout << "YES" << endl;
cout << ans.size() << endl;
for(const auto& v : ans)
cout << v << " ";
cout << endl;
}
else
cout << "NO" << endl;
return 0;
}