forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid-unlock-patterns.cpp
139 lines (122 loc) · 3.83 KB
/
android-unlock-patterns.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Time: O(9 * 2^9)
// Space: O(9 * 2^9)
// DP solution.
class Solution {
public:
int numberOfPatterns(int m, int n) {
// dp[i][j]: i is the set of the numbers in binary presentation,
// d[i][j] is the number of ways ending with the number j.
vector<vector<int>> dp(1 << 9 , vector<int>(9, 0));
for (int i = 0; i < 9; ++i) {
dp[merge(0, i)][i] = 1;
}
vector<int> keys(9, 0);
for (int used = 0; used < dp.size(); ++used) {
const auto number = number_of_key(used);
if (number > n) {
continue;
}
for (int i = 0; i < 9; ++i) {
if (!contain(used, i)) {
continue;
}
keys[number - 1] += dp[used][i];
const auto x1 = i / 3;
const auto y1 = i % 3;
for (int j = 0; j < 9; ++j) {
if (contain(used, j)) {
continue;
}
const auto x2 = j / 3;
const auto y2 = j % 3;
if (((x1 == x2 && abs(y1 - y2) == 2) ||
(y1 == y2 && abs(x1 - x2) == 2) ||
(abs(x1 - x2) == 2 && abs(y1 - y2) == 2)) &&
!(contain(used, convert((x1 + x2) / 2, (y1 + y2) / 2)))) {
continue;
}
dp[merge(used, j)][j] += dp[used][i];
}
}
}
int res = 0;
for (int i = m - 1; i < n; ++i) {
res += keys[i];
}
return res;
}
private:
inline int merge(int i, int j) {
return i | (1 << j);
}
inline int number_of_key(int i) {
int number = 0;
for (; i; i &= i - 1) {
++number;
}
return number;
}
inline bool contain(int i, int j) {
return i & (1 << j);
}
inline int convert(int i, int j) {
return 3 * i + j;
}
};
// Time: O(9!)
// Space: O(9)
// Backtracking solution.
class Solution2 {
public:
int numberOfPatterns(int m, int n) {
int number = 0;
bool visited[9] = {false};
// 1, 3, 7, 9
numberOfPatternsHelper(m, n, 0, 0, 1, visited, &number);
// 2, 4, 6, 8
numberOfPatternsHelper(m, n, 0, 1, 1, visited, &number);
number *= 4;
// 5
numberOfPatternsHelper(m, n, 1, 1, 1, visited, &number);
return number;
}
private:
const vector<vector<int>> directions = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {-1, -1}, {1, -1}, {-1, 1},
{2, 1}, {2, -1}, {-2, -1}, {-2, 1},
{1, 2}, {-1, 2}, {1, -2}, {-1, -2}
};
void numberOfPatternsHelper(int m, int n, int i, int j, int level,
bool visited[], int *number) {
if (level > n) {
return;
}
if (level >= m) {
++(*number);
}
visited[convert(i, j)] = true;
for (const auto& direction : directions) {
auto x = i + direction[0];
auto y = j + direction[1];
if (valid(x, y)) {
if (!visited[convert(x, y)]) {
numberOfPatternsHelper(m, n, x, y, level + 1, visited, number);
} else {
x += direction[0];
y += direction[1];
if (valid(x, y) && !visited[convert(x, y)]) {
numberOfPatternsHelper(m, n, x, y, level + 1, visited, number);
}
}
}
}
visited[convert(i, j)] = false;
}
inline int convert(int i, int j) {
return 3 * i + j;
}
inline bool valid(int i, int j) {
return i >= 0 && i < 3 && j >= 0 && j < 3;
}
};