-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12851.cpp
More file actions
59 lines (54 loc) · 1.22 KB
/
Copy path12851.cpp
File metadata and controls
59 lines (54 loc) · 1.22 KB
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 <stdio.h>
#include <queue>
#include <vector>
#define INF 987654321
#define MAX 100001
using namespace std;
vector<int> dist;
void solution(int N, int K, int &answer, int &cnt)
{
queue<int> pos;
pos.push(N);
dist[N] = 0;
while (!pos.empty())
{
int now = pos.front();
pos.pop();
if (now == K)
{
if (answer > dist[now])
{
answer = dist[now];
cnt = 1;
}
else if (answer == dist[now])
{
cnt++;
}
}
if (now < 100000 && dist[now + 1] >= dist[now] + 1)
{
dist[now + 1] = dist[now] + 1;
pos.push(now + 1);
}
if (now > 0 && dist[now - 1] >= dist[now] + 1)
{
dist[now - 1] = dist[now] + 1;
pos.push(now - 1);
}
if (now * 2 <= 100000 && dist[now * 2] >= dist[now] + 1)
{
dist[now * 2] = dist[now] + 1;
pos.push(now * 2);
}
}
}
int main()
{
int N, K, answer = INF, cnt = 0;
scanf("%d %d", &N, &K);
dist.assign(MAX, INF);
solution(N, K, answer, cnt);
printf("%d\n%d\n", answer, cnt);
return 0;
}