-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0935.cpp
More file actions
21 lines (19 loc) · 724 Bytes
/
Copy path0935.cpp
File metadata and controls
21 lines (19 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
static const int MOD = 1e9 + 7;
public:
int knightDialer(int n) const {
static const vector<int> offsets[] = {{4, 6}, {8, 6}, {7, 9}, {4, 8}, {3, 9, 0},
{}, {0, 1, 7}, {2, 6}, {1, 3}, {2, 4}};
vector<int> pdp(10, 1), dp(10);
for (int i = 1; i < n; i++) {
for (int j = 0; j < 10; j++) {
int res = 0;
for (const int prev : offsets[j])
res = (res + pdp[prev]) % MOD;
dp[j] = res;
}
swap(dp, pdp);
}
return accumulate(begin(pdp), end(pdp), 0, [&](int a, int acc) { return (acc + a) % MOD; });
}
};