-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[BOJ] 13549번.cpp
79 lines (61 loc) · 1.16 KB
/
[BOJ] 13549번.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
/*
* Problem: 13549
* Solver: Jinwon
* Reference: https://jdselectron.tistory.com/58
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int N, K;
struct cmp {
bool operator()(pair<int, int>& a, pair<int, int>& b) {
return a.second > b.second;
}
};
int BFS();
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pque;
int board[100001] = { 0 };
bool visited[100001] = { false };
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> K;
pque.push({ 0, N });
visited[N] = true;
cout << BFS();
return 0;
}
int BFS()
{
int time, index;
while (!pque.empty())
{
time = pque.top().first;
index = pque.top().second;
pque.pop();
if (index == K)
return time;
if (index < K)
{
if (index * 2 < 100001 && !visited[index * 2])
{
pque.push({ time, index * 2 });
visited[index * 2] = true;
}
if (!visited[index + 1])
{
pque.push({ time + 1, index + 1 });
visited[index + 1] = true;
}
}
if (index > 0 && !visited[index - 1])
{
pque.push({ time + 1, index - 1 });
visited[index - 1] = true;
}
}
}