-
Notifications
You must be signed in to change notification settings - Fork 0
/
1463.cpp
47 lines (45 loc) · 991 Bytes
/
1463.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
#include <iostream>
#include <queue>
using namespace std;
int dp[1000001];
queue<int> q;
int main(){
int X; cin >>X;
q.push(X);
dp[X]=1;
while(!q.empty()){
int v = q.front();
q.pop();
if(v%3==0){
if (dp[v/3]==0){
q.push(v/3);
dp[v/3]=dp[v]+1;
}
else if(dp[v/3]>dp[v]+1){
q.push(v/3);
dp[v/3]=dp[v]+1;
}
}
if(v%2==0){
if(dp[v/2]==0){
q.push(v/2);
dp[v/2]=dp[v]+1;
}
else if(dp[v/2]>dp[v]+1){
q.push(v/2);
dp[v/2]=dp[v]+1;
}
}
if(v-1!=0){
if(dp[v-1]==0){
q.push(v-1);
dp[v-1]=dp[v]+1;
}
else if(dp[v-1]>dp[v]+1){
q.push(v-1);
dp[v-1]=dp[v]+1;
}
}
}
cout << dp[1]-1;
}