-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12851.cpp
72 lines (67 loc) · 1.35 KB
/
12851.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
71
72
// 12851. 숨바꼭질 2
// 2019.08.05
// BFS
#include<iostream>
#include<queue>
using namespace std;
bool visit[200001]; // 방문 유무 저장
int dist[200001]; // dist[i] : i에 도착하는 가장 빠른 시간
long long cnt[200001]; // cnt[i] : 가장 빠른 시간으로 i를 찾는 방법의 개수
int main()
{
int n, k;
cin >> n >> k;
visit[n] = true;
queue<int> q;
q.push(n);
cnt[n] = 1;
while (!q.empty())
{
int now = q.front();
q.pop();
if (now - 1 >= 0) // x-1로 이동
{
if (visit[now - 1] == false)
{
q.push(now - 1);
visit[now - 1] = true;
dist[now - 1] = dist[now] + 1;
cnt[now - 1] = cnt[now];
}
else if (dist[now - 1] == dist[now] + 1)
{
cnt[now - 1] += cnt[now];
}
}
if (now + 1 <= 200000) // x+1로 이동
{
if (visit[now + 1] == false)
{
q.push(now + 1);
visit[now + 1] = true;
dist[now + 1] = dist[now] + 1;
cnt[now + 1] = cnt[now];
}
else if (dist[now + 1] == dist[now] + 1)
{
cnt[now + 1] += cnt[now];
}
}
if (now * 2 <= 200000) // 순간이동
{
if (visit[now * 2] == false)
{
q.push(now * 2);
visit[now * 2] = true;
dist[now * 2] = dist[now] + 1;
cnt[now * 2] = cnt[now];
}
else if (dist[now * 2] == dist[now] + 1)
{
cnt[now * 2] += cnt[now];
}
}
}
cout << dist[k] << "\n" << cnt[k]<<"\n";
return 0;
}