/
main.cpp
69 lines (63 loc) · 1.48 KB
/
main.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
62
63
64
65
66
67
68
69
//
// main.cpp
// 12852 1로 만들기 2
//
// Created by 이준영 on 2021/01/03.
//
#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
#include <vector>
using namespace std;
int N, d[] = {2,3};
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
pair<int,int> path[1000005];
void findPath(int idx){
if(path[idx].second == -1){
cout << idx << " ";
return;
}
findPath(path[idx].second);
cout << idx << " ";
}
void BFS(){
while(!pq.empty()){
int nowCnt = pq.top().first;
int nowIdx = pq.top().second;
pq.pop();
if(path[nowIdx].first < nowCnt){
continue;
}
for(int i = 0; i < 3; i++){
int nextIdx = -1, nextCnt;
if(i == 2){
nextIdx = nowIdx - 1;
}
else if(nowIdx % d[i] == 0){
nextIdx = nowIdx / d[i];
}
nextCnt = nowCnt + 1;
if(nextIdx == -1 or path[nextIdx].first <= nextCnt){
continue;
}
path[nextIdx] = {nextCnt,nowIdx};
pq.push({nextCnt,nextIdx});
}
}
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 0; i <= N; i++){
path[i] = {1000000000,-1};
}
pq.push({0,N});
path[N] = {0,-1};
BFS();
cout << path[1].first << "\n";
findPath(1);
return 0;
}