-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathm3d2.cpp
61 lines (57 loc) · 1.21 KB
/
m3d2.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 <map>
#include <queue>
#include <cassert>
using namespace std;
void showSolution(int target, map<int,int>& prev) {
int v = target;
string out = "";
while(prev[v] != - 1) {
out = (prev[v] * 3 == v ? "x3" : "/2") + out;
v = prev[v];
}
out = "1" + out;
int m = 1;
// evaluate the expression
for(size_t i=1; i<out.length(); i+=2) {
if (out[i]=='x')
m *= 3;
else
m /= 2;
}
assert(m == target);
cout << out << " = " << m <<endl;
}
void showStat(map<int,int>& prev) {
cout << "#entries in map = " << prev.size() << endl;
cout << "max value during search = " << (--prev.end())->first << endl;
}
void m3d2(int target) {
map<int, int> prev;
queue<int> q;
int v = 1;
q.push(1); prev[1] = - 1;
while( !q.empty() ) {
v = q.front() ; q.pop();
if (v == target) break;
int v2 = v/2;
int v3 = v*3;
if (prev[v2] == 0) { //if we've never found v2
q.push(v2); prev[v2] = v;
}
if (prev[v3] == 0) { //if we've never found v3
q.push(v3); prev[v3] = v;
}
}
if (v == target) {
showSolution(target, prev);
showStat(prev);
}
}
int main() {
int n;
cout << "n = ";
cin >> n;
m3d2(n);
return 0;
}