-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1463.cpp
61 lines (49 loc) · 1012 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int N;
int ans = 987654321;
int number[1000000];
void input() {
cin >> N;
memset(number, -1, sizeof(number));
}
int sol() {
queue<int> q;
number[N] = 0;
q.push(N);
while(!q.empty()){
int p = q.front();
q.pop();
int cnt = number[p];
//cout << "number is " << p << " & count is " << cnt << endl;
if (p == 1) {
return number[p];
}
if (p % 3 == 0) {
if (number[p / 3] == -1) {
//cout << "push " << p / 3 << " | cnt is " << cnt + 1 << endl;
number[p / 3] = cnt + 1;
q.push(p / 3);
}
}if (p % 2 == 0) {
if (number[p / 2] == -1) {
//cout << "push " << p / 2 << " | cnt is " << cnt + 1 << endl;
number[p / 2] = cnt + 1;
q.push(p / 2);
}
}
if (number[p - 1] == -1) {
//cout << "push " << p-1 << " | cnt is " << cnt + 1 << endl;
number[p - 1] = cnt + 1;
q.push(p - 1);
}
//cout << endl;
}
}
int main() {
input();
cout << sol() << endl;
return 0;
}