-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1697.cpp
54 lines (50 loc) · 1.18 KB
/
1697.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
// 1697. 숨바꼭질
// 2020.07.18
// BFS
#include<iostream>
#include<queue>
using namespace std;
bool visit[200001]; // 방문 유무 저장
int dist[200001]; // dist[i] : i에 도착하는 가장 빠른 시간
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 - 1 >= 0) // x-1로 이동
{
if (visit[now - 1] == false)
{
q.push(now - 1);
visit[now - 1] = true;
dist[now - 1] = dist[now] + 1;
}
}
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;
}
}
if (now * 2 <= 200000) // 순간이동
{
if (visit[now * 2] == false)
{
q.push(now * 2);
visit[now * 2] = true;
dist[now * 2] = dist[now] + 1;
}
}
}
cout << dist[k] << endl;
return 0;
}