-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ 18235.cpp
More file actions
51 lines (48 loc) · 1.01 KB
/
BOJ 18235.cpp
File metadata and controls
51 lines (48 loc) · 1.01 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
#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
#include <cmath>
using namespace std;
int map[500001];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,a,b;
cin>>n>>a>>b;
int answer=-1;
queue <pair<int,int>> q;
q.push({a,0});
q.push({b,0});
while (!q.empty()){
int loc=q.front().first;
int day=q.front().second;
q.pop();
int dist=pow(2,day);
int next=loc+dist;
if (next<=n){
if (map[next]==day+1){
answer=day+1;
break;
}
else{
map[next]=day+1;
q.push({next,day+1});
}
}
next=loc-dist;
if (next>0){
if (map[next]==day+1){
answer=day+1;
break;
}
else{
map[next]=day+1;
q.push({next,day+1});
}
}
}
cout<<answer;
return 0;
}