-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path935.cpp
More file actions
27 lines (27 loc) · 692 Bytes
/
935.cpp
File metadata and controls
27 lines (27 loc) · 692 Bytes
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
const int mod = 1e9 + 7;
class Solution {
public:
int knightDialer(int N) {
vector<vector<int>> moves = {
{4,6},
{6,8}, {7,9}, {4,8},
{3,9,0},{},{1,7,0},
{2,6},{1,3}, {2,4}
};
int dp[N+1][10];
memset(dp,0,sizeof(dp));
for(int i=0; i<10; i++)
dp[1][i] = 1;
for(int i=2; i<=N; i++){
for(int j=0; j<10; j++){
for(int k:moves[j]){
dp[i][j] = (dp[i][j] + dp[i-1][k])%mod;
}
}
}
int res = 0;
for(int i=0; i<10; i++)
res = (res+dp[N][i])%mod;
return res;
}
};