-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_12851.java
More file actions
71 lines (59 loc) · 1.75 KB
/
boj_12851.java
File metadata and controls
71 lines (59 loc) · 1.75 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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_12851 {
static int N,K;
static int[] count=new int[100001];
static boolean[] visit=new boolean[100001];
static int time;
static int ans=0;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
K=Integer.parseInt(st.nextToken());
if(N==K){
System.out.println(0);
System.out.println(1);
}else{
bfs(N);
System.out.println(time);
System.out.println(ans);
}
}
private static void bfs(int start){
time=Integer.MAX_VALUE/16;
Queue<Integer> q=new LinkedList<>();
q.add(start);
count[start]=1;
while(!q.isEmpty()){
int cur=q.poll();
visit[cur]=true;
if(time<count[cur]){
return;
}
for(int i=0;i<3;i++){
int nxt;
if(i==0){
nxt=cur+1;
}else if(i==1){
nxt=cur-1;
}else{
nxt=cur*2;
}
if(nxt==K){
time=count[cur];
ans++;
}
if(nxt>=0 && nxt<100000 && (count[nxt]==0 || count[nxt]==count[cur]+1)){
q.add(nxt);
count[nxt]=count[cur]+1;
}
}
}
}
}