-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1697.cpp
41 lines (38 loc) · 1.04 KB
/
1697.cpp
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
#include <iostream>
#include <queue>
using namespace std;
const int MAX=100001;
bool visited[MAX];
int N,K;
void bfs(){
queue<pair<int, int> >q;
q.push(make_pair(N,0));
visited[N]=true;
while(!q.empty()){
int nextLocation=q.front().first;
int seconds=q.front().second;
q.pop();
if(nextLocation==K){
cout<<seconds<<"\n";
}
if(nextLocation+1<MAX&&0<=nextLocation&&!visited[nextLocation+1]){
q.push(make_pair(nextLocation+1,seconds+1));
visited[nextLocation+1]=true;
}
if(nextLocation-1<MAX&&0<=nextLocation-1&&!visited[nextLocation-1]){
q.push(make_pair(nextLocation-1, seconds+1));
visited[nextLocation-1]=true;
}
if(nextLocation*2<MAX&&0<=nextLocation*2&&!visited[nextLocation*2]){
q.push(make_pair(nextLocation*2,seconds+1));
visited[nextLocation*2]=true;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N>>K;
bfs();
}