-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.cpp
More file actions
35 lines (34 loc) · 664 Bytes
/
Copy path1697.cpp
File metadata and controls
35 lines (34 loc) · 664 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
#include<stdio.h>
#include<queue>
using namespace std;
void check(int);
int bfs();
queue<int> q;
int N, K;
int isVisited[100001];
int main(){
scanf("%d%d", &N, &K);
printf("%d", bfs());
}
int bfs(){
q.push(N);
int depth = 0;
while (!q.empty()){
int s = q.size();
for (int i = 0; i < s; ++i){
int cur = q.front();
q.pop();
if (cur == K) return depth;
check(cur+1);
check(cur-1);
check(cur*2);
}
++depth;
}
}
void check(int to){
if (to >= 0 && to <= 100000 && !isVisited[to]){
isVisited[to] = 1;
q.push(to);
}
}