-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_08.c
More file actions
86 lines (64 loc) · 1.71 KB
/
Copy path5_08.c
File metadata and controls
86 lines (64 loc) · 1.71 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
73
74
75
76
77
78
79
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
int N,M;
int result_cnt = 0;
int check[100100];
struct pos{
int first;
int second;
};
int find(){
int result = 200000;
bool is_it_over = false;
queue<pos> pq;
pos now,new1;
now.first = N;
now.second = 0;
pq.push(now);
check[N] = 0;
while(!pq.empty()){
now = pq.front();
pq.pop();
if(result < now.second) continue;
if(now.first == M){
is_it_over = true;
result_cnt++;
result = now.second;
}
if(is_it_over) continue;
if(now.first-1 >= 0) {
new1.first = now.first-1;
new1.second = now.second+1;
if(check[new1.first] == -1 || check[new1.first] == new1.second){
pq.push(new1);
check[new1.first] = new1.second;
}
}
if(now.first+1 <= 100000){
new1.first = now.first+1;
new1.second = now.second+1;
if(check[new1.first] == -1 || check[new1.first] == new1.second){
pq.push(new1);
check[new1.first] = new1.second;
}
}
if(now.first*2 <= 100000) {
new1.first = now.first*2;
new1.second = now.second+1;
if(check[new1.first] == -1 || check[new1.first] == new1.second){
pq.push(new1);
check[new1.first] = new1.second;
}
}
}
return result;
}
int main(void){
cin>>N>>M;
memset(check,-1,sizeof(check));
cout<<find()<<"\n"<<result_cnt;
}