-
Notifications
You must be signed in to change notification settings - Fork 0
/
숨바꼭질.cpp
51 lines (46 loc) · 1 KB
/
숨바꼭질.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
#include "pch.h"
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, k;
bool visit[100001];
int min_n= 2100000000;
queue<pair<int, int>> q;
void bfs(int s, int count) {
pair<int, int> p = { s, count };
q.push(p);
visit[s] = true;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (cur.first == k) {
if (min_n > cur.second) {
min_n = cur.second;
}
}
if (cur.second >= min_n) {
continue;
}
if (cur.first + 1 <= 100001 && !visit[cur.first + 1]) {
visit[cur.first + 1] = true;
q.push(make_pair(cur.first + 1, cur.second + 1));
}
if (cur.first - 1 >= 0 && !visit[cur.first - 1]) {
visit[cur.first - 1] = true;
q.push(make_pair(cur.first - 1, cur.second + 1));
}
if (cur.first * 2 < 100001 && !visit[cur.first * 2]) {
visit[cur.first * 2] = true;
q.push(make_pair(cur.first * 2, cur.second + 1));
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
bfs(n, 0);
cout << min_n;
return 0;
}