-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.cpp
More file actions
39 lines (39 loc) · 964 Bytes
/
Copy path1697.cpp
File metadata and controls
39 lines (39 loc) · 964 Bytes
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
#include <iostream>
#include <queue>
#define MAX 100001
using namespace std;
int bfs(int start,int end){
queue<pair<int, int> > q;
bool visited[MAX] = {false};
q.push(make_pair(start, 0));
visited[start] = true;
while(!q.empty()){
int n = q.front().first;
int s = q.front().second;
if(n == end){
return s;
}
q.pop();
if(visited[2*n] == false &&2*n >=0 &&2*n < MAX){
visited[2*n] = true;
q.push(make_pair(2*n,s+1));
}
if(visited[n+1] ==false && n+1 >=0 && n+1 < MAX){
visited[n+1] = true;
q.push(make_pair(n+1,s+1));
}
if(visited[n-1] == false && n-1 >= 0 &&n-1 < MAX){
visited[n-1] =true;
q.push(make_pair(n-1,s+1));
}
}
return -1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
cout <<bfs(n,k);
return 0;
}