-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1697.cpp
56 lines (45 loc) · 950 Bytes
/
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
55
56
//1697 숨바꼭질
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
int MAX_RANGE = 100000;
bool vis[100000];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin >> n >> k;
queue<pair<int,int>> q;
q.push({0,n});
vis[n] = 1;
int i = 0;
while(!q.empty())
{
auto cur = q.front();
q.pop();
int now = cur.second;
if(now == k)
{
cout << cur.first;
break;
}
int nt = cur.first+1;
if(now - 1 >= 0 && vis[now-1]!=1)
{
q.push({nt,now-1});
vis[now-1] = 1;
}
if(now + 1 <= MAX_RANGE && vis[now+1] != 1)
{
q.push({nt,now+1});
vis[now+1] = 1;
}
if(now*2 <= MAX_RANGE && vis[now*2] != 1)
{
q.push({nt, now*2});
vis[now*2] = 1;
}
}
return 0;
}