-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13549.cpp
More file actions
56 lines (54 loc) · 1.16 KB
/
13549.cpp
File metadata and controls
56 lines (54 loc) · 1.16 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
// 13549. 숨바꼭질 3
// 2020.06.12
// BFS
#include<iostream>
#include<queue>
using namespace std;
int sec[200001];
bool visit[200001]; // 방문 유무 저장
int main()
{
int n, k;
cin >> n >> k;
visit[n] = true;
queue<int> q;
q.push(n);
while (!q.empty())
{
int now = q.front();
q.pop();
// 순간이동
if (now * 2 <= 200000)
{
if (visit[now * 2] == false)
{
q.push(now * 2);
visit[now * 2] = true;
sec[now * 2] = sec[now];
}
}
// x-1로 이동
if (now - 1 >= 0)
{
if (visit[now - 1] == false)
{
q.push(now - 1);
visit[now - 1] = true;
sec[now - 1] = sec[now] + 1;
}
}
// x+1로 이동
if (now + 1 <= 200000)
{
if (visit[now + 1] == false)
{
q.push(now + 1);
visit[now + 1] = true;
sec[now + 1] = sec[now] + 1;
}
}
}
// 결과 출력
cout << sec[k] << endl;
return 0;
}