935. 骑士拨号器
思路:将0-9分为四个部分:{1,3,7,9},{4,6},{2,8},{0}
代码
#include<map>
#include<vector>
#include<iostream>
using namespace std;
long M = 1e9 + 7;
int knightDialer(int N) {
if(N == 1){
return 10;
}
vector<long> dp1(4);
dp1[0] = 4;
dp1[1] = 2;
dp1[2] = 2;
dp1[3] = 1;
vector<long> dp2(4);
int step =1;
while(step<N){
dp2[0] = ((dp1[1] + dp1[2])<<1)%M;
dp2[1] = (dp1[0] + (dp1[3]<<1))%M;
dp2[2] = (dp1[0])%M;
dp2[3] = (dp1[1])%M;
dp1 = dp2;
step++;
}
long result = 0;
for(int i=0;i<dp1.size();i++){
result += dp1[i];
}
return result%M;
}
int main(){
int resultValue = knightDialer(161);
cout<<resultValue<<endl;
return 0;
}