109th LeetCode Weekly Contest Knight Dialer
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;
}
};
109th LeetCode Weekly Contest Knight Dialer的更多相关文章
- 109th LeetCode Weekly Contest Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- opennebula 对接创建模板参数
{ "id": 8, "name": "c5d1390c-1930-45a5-a686-5cef38b319d7", "displ ...
- c语言入门教程
https://www.youtube.com/playlist?list=PLY_qIufNHc293YnIjVeEwNDuqGo8y2Emx 感觉这个教程不错
- 《架构师杂志》评述:Scott Guthrie
发布日期: 2007-03-29 | 更新日期: 2007-03-29 Scott Guthrie 是 Microsoft 开发事业部的总经理.他领导着负责构建 CLR(公共语言运行库).ASP. ...
- 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null
建议53:必要时应将不再使用的对象引用赋值为null 在CLR托管的应用程序中,存在一个“根”的概念,类型的静态字段.方法参数.以及局部变量都可以作为“根”的存在(值类型不能作为“根”,只有引用类型的 ...
- 如何恢复VS2013代码实时校验功能
VS2013在某一天突然无法进行实时代码校验了,只有在编译的时候,错误列表才显示语法错误 怎么来解决这个问题呢?试试环境重置吧. 首先:打开工具菜单,选择“导入和导出设置”. 其次:可以先导出选定 ...
- 定制JMeter取样器
JMeter运行你区定义自己的取样器sampler,方法很简单,只需继承类org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClie ...
- 【转】android中如何实现离线缓存
原文地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1209/2136.html 离线缓存就是在网络畅通的情况下将从服务器收 ...
- SharePoint 2013报错之“指定的文件不是有效的电子表格或者没有包含要导入的数据”
当你尝试用SharePoint 2013中的“导入电子表格”功能时,可能会遇到报错“指定的文件不是有效的电子表格或者没有包含要导入的数据” 解决方法:只需要将你的SharePoint网址添加到浏览器的 ...
- .Net高级面试宝典
1.in/exists/join 执行效率? 答:用法 select * from HK_UsersBasic where Users_ID in (select AccEmail from dbo ...
- laravel中get方式表单提交后, 地址栏数据重复的问题
csrf_field这个要放form表单下面第一行的位置