-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_13913
More file actions
72 lines (62 loc) · 1.94 KB
/
BAEKJOON_13913
File metadata and controls
72 lines (62 loc) · 1.94 KB
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
import java.io.*;
import java.sql.Array;
import java.util.*;
public class Main {
static int MAX=100001;
static int N;
static int K;
static BufferedWriter bw;
static BufferedReader br;
public static void Print(int[] from,int n,int m) throws IOException{
if(n!=m){
Print(from,n,from[m]);
}
bw.write(m + " ");
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
Queue<Integer> q = new LinkedList<>();
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
int[] from = new int[100001];
boolean[] v = new boolean[1000001];
int[] dist = new int[100001];
dist[N] = 0;
v[N]=true;
q.add(N);
while (!q.isEmpty()) {
int now = q.poll(); //처음에 5를 꺼낸다.
if(now -1 >=0){
if (!v[now - 1]) {
v[now-1]=true;
q.add(now - 1);
dist[now-1]=dist[now]+1;
from[now-1] = now;
}
}
if (now + 1 < MAX) {
if (!v[now + 1]) {
v[now+1]=true;
q.add(now + 1);
dist[now + 1] = dist[now] + 1;
from[now+1] = now;
}
}
if (2 * now < MAX) {
if (!v[2 * now]) {
v[2 * now] = true;
q.add(2 * now);
dist[2 * now] = dist[now] + 1;
from[2*now] = now;
}
}
}
bw.write(String.valueOf(dist[K]));
bw.newLine();
Print(from,N,K);
bw.flush();
bw.close();
}
}