diff --git "a/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.cpp" "b/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.cpp" new file mode 100644 index 0000000..67c4dac --- /dev/null +++ "b/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.cpp" @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; + +const int MAX = 200001; +int n, k; +bool check[MAX]; +int dist[MAX]; +int from[MAX]; + +void print(int n, int m) { + if(n != m) { + print(n, from[m]); + } + cout << m << " "; +} + +void bfs() +{ + queue q; + q.push(n); + check[n] = true; + dist[n] = 0; + + while (!q.empty()) + { + int now = q.front(); + if (now == k) + { + break; + } + q.pop(); + + if (now - 1 >= 0 && check[now - 1] == false) + { + q.push(now - 1); + dist[now - 1] = dist[now] + 1; + check[now - 1] = true; + from[now - 1] = now; + } + + if (now + 1 < MAX && check[now + 1] == false) + { + q.push(now + 1); + dist[now + 1] = dist[now] + 1; + check[now + 1] = true; + from[now + 1] = now; + } + + if (now * 2 <= MAX && check[now * 2] == false) + { + q.push(now * 2); + dist[now * 2] = dist[now] + 1; + check[now * 2] = true; + from[now * 2] = now; + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> n >> k; + bfs(); + + cout << dist[k] << endl; + print(n, k); + return 0; +} \ No newline at end of file diff --git "a/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.py" "b/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.py" new file mode 100644 index 0000000..c0b8eb5 --- /dev/null +++ "b/\352\267\270\353\236\230\355\224\204/\354\210\250\353\260\224\352\274\255\354\247\2104/baekjoon-13913.py" @@ -0,0 +1,36 @@ +from collections import deque + +MAX = 200001 + + +def bfs(n, k): + q = deque() + q.append(n) + dist = {} + while q: + now = q.popleft() + if now == k: + break + + for next in [now - 1, now + 1, now * 2]: + if 0 <= next <= MAX and next not in dist: + q.append(next) + dist[next] = now + return dist + + +def solution(): + n, k = map(int, input().split()) + move_dict = bfs(n, k) + + ans = [] + while k != n: + ans.append(k) + k = move_dict[k] + ans.append(n) + + print(len(ans) - 1) + print(' '.join(map(str, ans[::-1]))) + + +solution()