-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12851.cpp
More file actions
55 lines (55 loc) · 1.18 KB
/
12851.cpp
File metadata and controls
55 lines (55 loc) · 1.18 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
#include<iostream>
#include<queue>
#include<vector>
#define MAX 100001
#define INF 0x3f3f3f3f
using namespace std;
int d[MAX];
int cnt=0,minVal=INF;
int main(){
int start,end;
cin>>start>>end;
for(int i=0;i<MAX;i++){
d[i]=INF;
}
queue<pair<int,int>> q;
q.push({start,0});
d[start]=0;
while(!q.empty()){
int cur = q.front().first;
int cost = q.front().second;
q.pop();
if(cur==end){
if(minVal==cost){
cnt++;
}
else if(minVal>cost){
minVal=cost;
cnt=1;
}
continue;
}
if(d[cur]!=cost){
continue;
}
if(cur+1<MAX){
if(d[cur+1]>=cost+1){
d[cur+1]=cost+1;
q.push({cur+1,cost+1});
}
}
if(cur-1>=0){
if(d[cur-1]>=cost+1){
d[cur-1]=cost+1;
q.push({cur-1,cost+1});
}
}
if(cur*2<MAX){
if(d[cur*2]>=cost+1){
d[cur*2]=cost+1;
q.push({cur*2,cost+1});
}
}
}
cout<<minVal<<"\n"<<cnt;
}