A chess knight can move as indicated in the chess diagram below:

 .           

This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops.  Each hop must be from one key to another numbered key.

Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total.

How many distinct numbers can you dial in this manner?

Since the answer may be large, output the answer modulo 10^9 + 7.

Example 1:

Input: 1
Output: 10

Example 2:

Input: 2
Output: 20

Example 3:

Input: 3
Output: 46

Note:

  • 1 <= N <= 5000

说就是一个马在键盘上跳,能得到多少种的数字,比如1就是不跳。。在原地就是0到9,10个数

那么可以看看,0这个位置只有4和6可以跳过来,1是6,8可以跳过来,5是不可能到的,所以就有下面的代码(参考别人的,自己写的不好看)

class Solution {
public:
int mod = ;
int dp[],dp2[];
int knightDialer(int N) {
for(int i=;i<=;i++){
dp[i]=;
}
for(int i=;i<N;i++){
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = ((dp[]+dp[])%mod+dp[])%mod;
dp2[] = ;
dp2[] = ((dp[]+dp[])%mod+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
for(int j=;j<=;j++){
dp[j] = dp2[j];
}
}
int result = ;
for(int i=;i<=;i++){
cout<<dp[i]<< " ";
result += dp[i] %mod;
result %= mod;
}
cout<<endl;
return result;
}
};

最新文章

  1. CSS之A标签
  2. 如何利用tomcat和cas实现单点登录(1):配置tomcat的ssl和部署cas
  3. c++中两个类互相引用的问题
  4. C# 给软件加注册码功能
  5. css笔记12:块元素和行内元素
  6. CentOS下Redis 2.2.14安装配置详解(转载)
  7. 参数化时按行读取txt文件,如何去掉换行符&quot;\n&quot;
  8. centos下一个bash: XXX: command not found解决方案
  9. 关于第一次STM32连接电脑下载程序
  10. Session、Cookie 学习笔记
  11. window.open()参数详解及对浏览器的兼容性
  12. java实现 批量转换文件编码格式
  13. 【C语音基础】printf()用法
  14. Linux下weblogic10.3.6(jar)版本安装详解
  15. ZOJ 3690 Choosing number(矩阵)
  16. How to use external classes and PHP files in Laravel Controller?
  17. 微擎开启redis memcache
  18. Spring Data JPA @Column 注解无效 打出的语句有下划线
  19. C++11 std::shared_ptr总结与使用
  20. Java如何删除数组中的元素?

热门文章

  1. 面试题:filter过滤器 listener 监听器 案例有点用
  2. Python学友
  3. CentOS 安装mongodb3.0 二进制包
  4. win32多线程 (四) Mutex
  5. Linux 查看是64位还是32位
  6. POJ3259 Wormholes(SPFA判断负环)
  7. web集群时session同步的3种方法
  8. Vue vue-resource发送Http请求
  9. 申请免费通配符证书(Let&#39;s Encrypt)并绑定IIS(转载)
  10. 「POJ 1741」Tree