-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210316_BOJ_13913.cpp
More file actions
70 lines (51 loc) · 970 Bytes
/
Copy path210316_BOJ_13913.cpp
File metadata and controls
70 lines (51 loc) · 970 Bytes
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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int Max = 100000 + 1;
int from, to, cnt;
vector<int> v(Max, 0);
bool visit[Max]{ false };
queue<pair<int,int>> q;
int dir[3]{ 2, -1, 1 };
vector<int> res;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> from >> to;
q.push({ from , 0 });
while (!q.empty()) {
int x = q.front().first;
cnt = q.front().second;
q.pop();
if (x == to)
break;
for (int i = 0; i < 3; ++i) {
int n_x;
if (i == 0) {
n_x = x * dir[i];
}
else {
n_x = x + dir[i];
}
if (0 > n_x || n_x > Max)
continue;
if (visit[n_x])
continue;
visit[n_x] = true;
q.push({ n_x , cnt + 1 });
v[n_x] = x;
}
}
int index = to;
res.push_back(to);
while (index != from) {
res.push_back(v[index]);
index = v[index];
}
cout << cnt << "\n";
for (int i = res.size() - 1; i >= 0; --i)
cout << res[i] << " ";
cout << "\n";
return 0;
}