-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumberOfPatterns.cpp
More file actions
33 lines (33 loc) · 1.18 KB
/
numberOfPatterns.cpp
File metadata and controls
33 lines (33 loc) · 1.18 KB
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
class Solution {
public:
int numberOfPatterns(int m, int n) {
int res = 0;
vector<bool> visited(10, false);
vector<vector<int>> jumps(10, vector<int>(10, 0));
jumps[1][3] = jumps[3][1] = 2;
jumps[4][6] = jumps[6][4] = 5;
jumps[7][9] = jumps[9][7] = 8;
jumps[1][7] = jumps[7][1] = 4;
jumps[2][8] = jumps[8][2] = 5;
jumps[3][9] = jumps[9][3] = 6;
jumps[1][9] = jumps[9][1] = jumps[3][7] = jumps[7][3] = 5;
res += helper(1, 1, 0, m, n, jumps, visited) * 4;
res += helper(2, 1, 0, m, n, jumps, visited) * 4;
res += helper(5, 1, 0, m, n, jumps, visited);
return res;
}
int helper(int num, int len, int res, int m, int n, vector<vector<int>> &jumps, vector<bool> &visited) {
if (len >= m) ++res;
++len;
if (len > n) return res;
visited[num] = true;
for (int next = 1; next <= 9; ++next) {
int jump = jumps[num][next];
if (!visited[next] && (jump == 0 || visited[jump])) {
res = helper(next, len, res, m, n, jumps, visited);
}
}
visited[num] = false;
return res;
}
};