-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.cpp
More file actions
64 lines (59 loc) · 1.6 KB
/
Copy path1697.cpp
File metadata and controls
64 lines (59 loc) · 1.6 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
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
#define FastIO\
ios_base::sync_with_stdio(false);\
cin.tie(NULL);\
cout.tie(NULL);
using namespace std;
int main(){
FastIO;
int n,k;
cin>>n>>k;
if(k==n){
cout<<0;
return 0;
}
queue<pair<int,int>> q;//점, 시간
bool visited[100001] ={};//false로 초기화 {false}도 가능
q.push({n,0});
visited[n] = true;
while(!q.empty()){
pair<int,int> start = q.front();
if(start.first==k){
cout<<start.second;
return 0;
}
q.pop();
//걷는경우
if(start.first+1<=100000){
if(!visited[start.first+1]){
q.push({start.first+1, start.second+1});
visited[start.first+1] = true;
if(start.first+1 == k){
cout<<start.second+1;
return 0;
}
}
}
if(start.first-1>=0){
if(!visited[start.first-1]){
q.push({start.first-1, start.second+1});
visited[start.first-1] = true;
if(start.first-1 == k){
cout<<start.second+1;
return 0;
}
}
}
//순간이동하는 경우
if(start.first*2<=100000){
if(!visited[start.first*2]){
q.push({start.first*2, start.second+1});
visited[start.first*2] = true;
if(start.first*2 == k){
cout<<start.second+1;
return 0;
}
}
}
}
}