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 ...
随机推荐
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- 243. Shortest Word Distance 最短的单词index之差
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- ls -al
ls -al:显示当前文件下所有的文件
- django 1.8 官方文档翻译:13-12 验证器
django 1.8 官方文档翻译:13-12 验证器 2015年09月20日 21:36:18 ApacheCN_飞龙 阅读数:639 https://blog.csdn.net/wizardfo ...
- MongoDB整理笔记の进程控制
查看活动进程 > db.currentOp(); > // 等同于: db.$cmd.sys.inprog.findOne() { inprog: [ { "opid" ...
- GCC 显示所有的warning option
此事需求的来源是有要求调查,gcc中的那些warning是有问题的,代码必须要修改,那些事需要确认的,就要显示所有的warning选项是什么 查了下,方法如下 显示所有的warning和warning ...
- 客户端 post ,get 访问服务器
private void sendReuestExpansion() { HttpRequest<T> req = this; HttpWebRequest request; try { ...
- Autofac的Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误解决方案。
在使用Autofac的时候,不给力,看着例子来的,人家没问题,我就报了Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误. 百般 ...
- cinder create volume的流程-scheduler调度
创建 Volume 时,cinder-scheduler 会基于容量.Volume Type 等条件选择出最合适的存储节点,然后让其创建 Volume. 1.cinder-scheduler配置相关项 ...
- [Algorithm]图
一.图的算法 邻接矩阵表示的数据结构 1 #define INFINITY INT_MAX // 无穷大 2 #define MAX_VERTEX_NUM 20 // 限制顶点最大数值为20个 3 # ...