-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKU 3278BYL.cpp
47 lines (45 loc) · 977 Bytes
/
PKU 3278BYL.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, m, t;
int d[100110]={0};
int f[100110]={0};
void bfs()
{
int op = 1, cl = 1, x = n; //头尾 x现在在的位置
memset(d, 44, sizeof(d));
memset(f, 0, sizeof(f));
d[n] = 0; //走过的长度
f[1]=n; //队列
while(op >= cl)
{
x = f[cl];
if(x - 1 >= 0 && d[x - 1] > d[x] + 1)
{
d[x - 1] = d[x] +1;
f[++ op] = x - 1;
}
if(x + 1 <= 100100 && d[x + 1] > d[x] + 1)
{
d[x + 1] = d[x] +1;
f[++ op] = x + 1;
}
if(x * 2 <= 100100 && d[x * 2] > d[x] + 1)
{
d[x * 2] = d[x] +1;
f[++ op] = x * 2;
}
cl ++;
}
}
int main()
{
scanf("%d", &t);
while(t --)
{
scanf("%d %d", &n, &m);
bfs();
printf("%d\n", d[m]);
}
return 0;
}