-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj1697.cpp
More file actions
executable file
·47 lines (40 loc) · 765 Bytes
/
Copy pathbj1697.cpp
File metadata and controls
executable file
·47 lines (40 loc) · 765 Bytes
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
// 숨바꼭질
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int MAX = 100001;
int n, k, answer;
int dist[MAX];
bool visited[MAX] = {false};
int a[3];
queue<int> q;
void bfs(){
int v;
q.push(n);
visited[n] = true;
while (!q.empty()){
v = q.front();
q.pop();
if (v == k){
answer = dist[v];
return;
}
a[0] = v+1;
a[1] = v-1;
a[2] = v*2;
for (int i: a){
if (i < MAX && i >= 0 && !visited[i]){
q.push(i);
visited[i] = true;
dist[i] = dist[v] + 1;
}
}
}
}
int main(){
cin >> n >> k;
bfs();
cout << answer << endl;
return 0;
}