-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_2001.cpp
75 lines (70 loc) · 2.16 KB
/
problem_2001.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
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
queue<int> q;
set<int> used;
map<int, int> path;
bool contains_zero(int num) {
if(to_string(num).find_first_of('0') != string::npos) return true;
return false;
}
void add_neighbors_to_queue(int num) {
int current_num;
if(num / 1000 < 9) if(!used.count(num + 1000)) {
if(!contains_zero(num + 1000)) {
q.push(num + 1000); // 1 цифра + 1
path.insert(pair<int, int>(num + 1000, num));
}
}
if(num % 1000 > 1) if(!used.count(num - 1)) {
if(!contains_zero(num - 1)) {
q.push(num - 1); // Последняя цифра - 1
path.insert(pair<int, int>(num - 1, num));
}
}
current_num = num % 1000 % 100 % 10 * 1000 + num / 1000 * 100 + num % 1000 / 100 * 10 + num % 1000 % 100 / 10;
if(!used.count(current_num)) {
if(!contains_zero(current_num)) {
q.push(current_num); // Сдвиг вправо
path.insert(pair<int, int>(current_num, num));
}
}
current_num = num % 1000 / 100 * 1000 + num % 1000 % 100 / 10 * 100 + num % 1000 % 100 % 10 * 10 + num / 1000;
if(!used.count(current_num)) {
if(!contains_zero(current_num)) {
q.push(current_num); // Сдвиг влево
path.insert(pair<int, int>(current_num, num));
}
}
}
void process_for(int to) {
int current_elem;
while(!q.empty()) {
current_elem = q.front();
q.pop();
used.insert(current_elem);
// cout << current_elem << " ";
if(current_elem == to) {
break;
}
add_neighbors_to_queue(current_elem);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int from, to;
cin >> from >> to;
add_neighbors_to_queue(from);
process_for(to);
vector<int> local_path;
while(to != from) {
local_path.push_back(to);
to = path[to];
}
local_path.push_back(to);
reverse(local_path.begin(), local_path.end());
for(auto path_item : local_path) {
cout << path_item << "\n";
}
}