-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsealTheSafe.hpp
75 lines (69 loc) · 2.86 KB
/
UnsealTheSafe.hpp
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
70
71
72
73
74
75
#include <iostream>
#include <map>
#include <utility>
using namespace std;
class UnsealTheSafe
{
public:
map<pair<int,int>,long> m;
long count = 0;
long countPasswords(int N) {
long levelsLeft = N-1;
count = nextNumber(0, levelsLeft) + nextNumber(1, levelsLeft) + nextNumber(2, levelsLeft) + nextNumber(3, levelsLeft) + nextNumber(4, levelsLeft) + nextNumber(5, levelsLeft) + nextNumber(6, levelsLeft) + nextNumber(7, levelsLeft) + nextNumber(8, levelsLeft) + nextNumber(9, levelsLeft);
return count;
}
// Recursive function to count all possible password combinations of length N and returns count
long nextNumber(int num, long levelsLeft) {
// base case: If we have achieved N digits, then this is one combination
if (levelsLeft == 0) {
return 1;
}
// Check if value is already in map otherwise add to map
auto key = make_pair(num, levelsLeft);
int found = m.count(key);
// already calculated so just return the value
if (found == 1) {
// cout << "found key" << endl;
return m[key];
}
// Call recursive function for each number adjacent to num
switch (num) {
case 0:
count = nextNumber(7, levelsLeft-1);
break;
case 1:
count = nextNumber(2, levelsLeft-1) + nextNumber(4, levelsLeft-1);
break;
case 2:
count = nextNumber(1, levelsLeft-1) + nextNumber(5, levelsLeft-1) + nextNumber(3, levelsLeft-1);
break;
case 3:
count = nextNumber(2, levelsLeft-1) + nextNumber(6, levelsLeft-1);
break;
case 4:
count = nextNumber(1, levelsLeft-1) + nextNumber(5, levelsLeft-1) + nextNumber(7, levelsLeft-1);
break;
case 5:
count = nextNumber(2, levelsLeft-1) + nextNumber(4, levelsLeft-1) + nextNumber(6, levelsLeft-1) + nextNumber(8, levelsLeft-1);
break;
case 6:
count = nextNumber(3, levelsLeft-1) + nextNumber(5, levelsLeft-1) + nextNumber(9, levelsLeft-1);
break;
case 7:
count = nextNumber(4, levelsLeft-1) + nextNumber(8, levelsLeft-1) + nextNumber(0, levelsLeft-1);
break;
case 8:
count = nextNumber(5, levelsLeft-1) + nextNumber(7, levelsLeft-1) + nextNumber(9, levelsLeft-1);
break;
case 9:
count = nextNumber(6, levelsLeft-1) + nextNumber(8, levelsLeft-1);
break;
}
// map doesn't have the value so store it in map
if (found == 0) {
// cout << "add key " << key.first << " " << key.second << endl;
m[key] = count;
}
return count;
}
};