-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1697.cpp
59 lines (53 loc) · 1.08 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int n, k;
bool visit[100001];
void bfs(int a)
{
queue<pair<int, int>> q;
q.push(make_pair(a, 0));
while (!q.empty())
{
int x = q.front().first;
int cnt = q.front().second;
q.pop();
if (x == k)
{
cout << cnt;
break;
}
if (x + 1 >= 0 && x + 1 < 100001)
{
if (!visit[x + 1])
{
visit[x + 1] = true;
q.push(make_pair(x + 1, cnt + 1));
}
}
if (x - 1 >= 0 && x - 1 < 100001)
{
if (!visit[x - 1])
{
visit[x - 1] = true;
q.push(make_pair(x - 1, cnt + 1));
}
}
if (2 * x >= 0 && 2 * x < 100001)
{
if (!visit[2 * x])
{
visit[2 * x] = true;
q.push(make_pair(2 * x, cnt + 1));
}
}
}
}
int main()
{
cin >> n >> k;
visit[n] = true;
bfs(n);
return 0;
}