-
Notifications
You must be signed in to change notification settings - Fork 1
/
1697.cpp
46 lines (43 loc) · 820 Bytes
/
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
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
bool chk[222222] = { false, };
int main()
{
int subin, sibling, idx = 0, cnt = 0, now;
queue<int> q[2];
cin >> subin >> sibling;
if (subin == sibling) goto end;
q[idx].push(subin);
while (!q[idx].empty())
{
cnt++;
while (!q[idx].empty())
{
now = q[idx].front();
q[idx].pop();
if (now + 1 == sibling || now - 1 == sibling || now * 2 == sibling)
goto end;
if (now<sibling && !chk[now * 2])
{
q[!idx].push(now * 2);
chk[now * 2] = true;
}
if (now<sibling && !chk[now + 1])
{
q[!idx].push(now + 1);
chk[now + 1] = true;
}
if (now - 1 >= 0 && !chk[now - 1])
{
q[!idx].push(now - 1);
chk[now - 1] = true;
}
}
idx = !idx;
}
end:
cout << cnt << endl;
return 0;
}