-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_12851번.cpp
80 lines (58 loc) · 994 Bytes
/
BOJ_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
73
74
75
76
77
78
79
80
//숨바꼭질2 1697번
#include<iostream>
#include<queue>
using namespace std;
#define MAX_SIZE 100001
#define INF 987654321
int map[MAX_SIZE];
int visit[MAX_SIZE];
queue<int> q;
int n, k, sec, MIN, cnt;
void BFS() {
int size;
while (!q.empty()) {
size = q.size();
sec++;
for (int i = 0; i < size; i++) {
int now = q.front();
visit[now] = 1;
q.pop();
if (now == k) {
if (sec <= MIN) {
MIN = sec;
cnt++;
}
else return;
}
// X - 1
int next = now - 1;
if (0 <= next && visit[next] == 0) {
q.push(next);
}
// X + 1
next = now + 1;
if (next <= 100000 && visit[next] == 0) {
q.push(next);
}
// 2*X
next = now * 2;
if (next <= 100000 && visit[next] == 0) {
q.push(next);
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
MIN = INF;
sec = -1;
cnt = 0;
visit[n] = 1;
q.push(n);
BFS();
cout << MIN << '\n';
cout << cnt << '\n';
return 0;
}