-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (67 loc) · 1.22 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;
struct point
{
int x , step;
}st;
queue <point> q;
int vis[200000];
int n, m;
int bfs()
{
while (!q.empty())
{
q.pop();
}
memset(vis,0,sizeof(vis));
vis[st.x] = 1;
q.push(st);
while (!q.empty())
{
point now = q.front();
if(now.x == m)
return now.step;
q.pop();
for (int j = 0;j < 3; j++)
{
point next = now;
if(j == 0)
{
next.x = next.x + 1;
}
else if (j == 1)
{
next.x = next.x - 1;
}
else if (j == 2)
{
next.x = next.x * 2;
}
++next.step;
if (next.x == m)
{
return next.step;
}
if (next.x >= 0 && next.x <= 200000 && !vis[next.x])
{
vis[next.x] = 1;
q.push(next);
}
}
}
return 0;
}
int main()
{
while (~scanf("%d %d",&n,&m))
{
st.x = n;
st.step = 0;
printf("%d\n",bfs());
}
return 0;
}